Backtracking builds a solution one choice at a time and abandons a path the moment it cannot work. It explores a tree of partial choices, going deeper while things look valid and retreating when they do not.
The difference from brute force is when you check. Brute force generates every full candidate, then checks each one at the end. It never notices a doomed path early, so it wastes effort completing arrangements that were broken from the start. Backtracking tests constraints during construction and prunes dead branches before finishing them.
That early pruning is where the savings come from. On problems like placing queens or filling a grid, backtracking skips huge regions of hopeless combinations, while brute force builds and checks them all.
Rewriting in plainer words…
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Queen 1, column 2Each row of a 4 by 4 board gets one queen. No two queens may share a column or a diagonal.
Queen 2, column 1It sits diagonal to queen 1, so stop here.
16 skipped boardsBrute force fills rows 3 and 4 in all 16 ways.
Queen 2, column 2It shares queen 1's column, so its 16 boards are never built.
Queen 2, column 3It is on a diagonal of queen 1, so its 16 boards are never built.
Queen 2, column 4No queen attacks it, so the search goes deeper.
Queen 3, column 1This is the only safe spot in row 3.
Queen 4, column 3All four queens are safe. This is a solution.
1step
At the top: Queen 1, column 2 (Each row of a 4 by 4 board gets one queen. No two queens may share a column or a diagonal).
1 / 4← → keys
Backtracking stops as soon as a queen is attacked, so the 16 boards below each of the three cuts never get built. Brute force builds those 48 boards too, then checks each one.
Saved in this browser - sign in to keep your review list.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Keep going - a few more words and AI can grade it.
Interview lens
How interviewers actually use this question
Likely follow-ups
What does the code skeleton of a backtracking function look like?Name the choose, recurse, undo steps, the base case that records a solution, and where the validity check goes.
How would you stop a backtracking search from returning duplicate answers when the input has repeats?Sort the input, then skip a value equal to the one before it at the same depth, or keep a per-level set of values already tried.
Backtracking builds a solution one choice at a time and drops a path as soon as it can't lead to a valid answer.
I picture it as a tree of partial choices, where I go deeper while the partial solution is still valid and step back to try the next option when it isn't.
The real difference from brute force is when you check, because brute force generates every complete candidate first and only tests each one at the end.
So brute force keeps finishing arrangements that were broken from the first few choices, while backtracking checks constraints during construction and prunes those branches early.
With queens on a board, once a new queen attacks an earlier one I abandon that branch and never place the rest, which skips huge regions of hopeless combinations.
The savings depend on constraints failing early, though, so when they don't, backtracking can still explore an exponential number of paths.
Weak answers to avoid
Calls backtracking just recursion or DFSRecursion is how you usually write it, not what sets it apart. Name the constraint check during construction that prunes a branch before it is finished.
Defines backtracking but never contrasts brute forceThe question asks for the difference. Say brute force checks only complete candidates, while backtracking checks partial ones and drops them early.
Claims backtracking makes the problem fastPruning saves work only when constraints fail early. The worst case can still be exponential, so say it skips hopeless branches, not that it makes the problem easy.
Interview lens
Likely follow-ups, what you can say, and the weak answers to avoid.