Skip to interview prep
Apexed

8. What is backtracking? Give an example.

MediumRecursion & backtracking

Technical

Type or dictate your answer. Your draft is saved on this device.

Show model answer

Backtracking builds a solution step by step and abandons (prunes) a partial solution as soon as it cannot lead to a valid answer, then undoes the last choice and tries the next. It is a depth-first search over the space of choices.

Examples: generating subsets and permutations, N-Queens, Sudoku, and combination sums. The program prints all binary strings of length 3.

Example program

What is backtracking? Give an example. — Data structures & algorithms interview question — Apexed