Why Backtracking?
Systematic Search with Pruning
Backtracking explores all possible solutions by building candidates incrementally and abandoning ("backtracking") as soon as it determines a candidate cannot lead to a valid solution. Think of solving a maze: go forward, hit a wall, go back, try a different path.
The Universal Template
Almost every backtracking problem follows the same 3-step pattern: Choose (pick an option), Explore (recurse), Unchoose (undo and try next). Master the template and you can solve dozens of problems.
Interview Frequency
Backtracking appears in ~10-15% of coding interviews. Key problems: permutations, combinations, subsets, N-Queens, Sudoku, word search.
The Universal Backtracking Template
The 3-Step Pattern: Choose, Explore, Unchoose
Decision Tree: Permutations of [1, 2, 3]
Classic Backtracking Problems
Problem 1: Permutations (LeetCode 46)
Problem 2: Combinations (LeetCode 77)
Problem 3: Subsets (LeetCode 78)
Problem 4: N-Queens (LeetCode 51)
Problem 5: Word Search (LeetCode 79)
Permutations Step-by-Step Trace
N-Queens (n=4) Output
Pruning Strategies
- Sort the input: Sorting helps skip duplicates and enables early termination (e.g., if current sum exceeds target, stop).
- Constraint propagation: In N-Queens, tracking columns and diagonals in sets gives O(1) conflict checks.
- Skip duplicates: For problems with duplicate elements, sort first and skip
if i > start and nums[i] == nums[i-1]. - Bound checking: If remaining elements cannot possibly complete a valid solution, prune early.
Backtracking vs DFS vs DP
| Aspect | Backtracking | DFS | Dynamic Programming |
|---|---|---|---|
| Goal | Find all (or one) valid solutions | Traverse/search a graph | Find optimal value |
| Pruning | Yes, key feature | Rarely | N/A (memoization) |
| Undo step | Yes (unchoose) | No | No |
| Typical use | Permutations, combos, puzzles | Graph traversal, cycles | Optimization, counting |
Deep Dives
Deep Dive 1: Pruning Optimization
Effective pruning can reduce exponential time dramatically. For example, N-Queens without pruning explores n^n states; with column + diagonal pruning, it explores roughly n! states -- orders of magnitude fewer.
- Sort input arrays to enable duplicate skipping and early termination.
- Use bitmask instead of sets for columns/diagonals in N-Queens for constant-factor speedup.
- For constraint-heavy problems (Sudoku), apply constraint propagation before backtracking.
Deep Dive 2: Iterative vs Recursive Backtracking
Recursive backtracking is more intuitive but uses O(depth) call stack. Iterative approaches use an explicit stack, avoiding stack overflow for deep recursion. However, iterative backtracking is harder to write and rarely needed in interviews.
Common Mistakes
Mistake #1: Forgetting to Undo the Choice
Mistake #2: Not Handling Duplicates
Practice Problems
Easy: Letter Combinations of a Phone Number (LeetCode 17)
Given a string of digits 2-9, return all possible letter combinations that the number could represent (like a phone keypad).
Hint
Map each digit to its letters. Backtrack through each digit position, choosing one letter at a time.
Medium: Palindrome Partitioning (LeetCode 131)
Given a string, partition it so that every substring is a palindrome. Return all possible partitions.
Hint
At each position, try all substrings starting there. If a substring is a palindrome, choose it and recurse on the remainder.
Hard: Sudoku Solver (LeetCode 37)
Fill a 9x9 Sudoku board so each row, column, and 3x3 box contains digits 1-9.
Hint
Find the next empty cell. Try digits 1-9, checking row/col/box constraints. Backtrack if no digit works.
Quick Reference Guide
Key Takeaways
- Choose, Explore, Unchoose -- the universal backtracking template solves most problems.
- Pruning is critical -- sorting input and checking constraints early cuts branches dramatically.
- Permutations use a
usedarray; combinations/subsets use astartindex to avoid reuse.
Related Topics
Time Complexities
- Permutations: O(n! * n)
- Combinations: O(C(n,k) * k)
- Subsets: O(2^n * n)
- N-Queens: O(n!)