LearnThatStack Ace your next interview
Sorting, Searching & Recursion · question
Question 9 of 61

What is backtracking, and how does it differ from plain brute-force search?

beginner
← All Sorting, Searching & Recursion questions
Re-explain
Visual ↓

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.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for Sorting, Searching & Recursion One question at a time. Answer out loud, get graded, see what you missed.
See it - interactive diagram
Where backtracking cuts the 4-queens tree
  • 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.

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.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

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.
  • If pruning skips so much work, why is backtracking still exponential in the worst case? Pruning helps only when partial choices fail early. With weak constraints the tree stays near full size, and listing all subsets needs 2^n outputs.
  • 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.
  • How does branch-and-bound extend backtracking for optimization problems? Keep the best answer found so far and cut any branch whose bound shows it can't beat it, as well as branches that break a rule.

What you can say

  1. Backtracking builds a solution one choice at a time and drops a path as soon as it can't lead to a valid answer.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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 DFS Recursion 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 force The 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 fast Pruning 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.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Sorting, Searching & Recursion cheatsheet.

← Back to all Sorting, Searching & Recursion questions
Pro · $10/mo

51 of 61 Sorting, Searching & Recursion answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime