Section 4: Backtracking Patterns
In Section 3, we saw that greedy algorithms commit to the locally optimal choice and never reconsider. But many problems do not have a provably optimal local choice. When you need to explore all possible combinations, permutations, or arrangements to find valid solutions, you need backtracking.
Backtracking is systematic brute force. It builds a solution incrementally, one choice at a time. After making a choice, it explores the consequences. If the path leads to a dead end, it undoes the last choice and tries the next option. This “try, explore, undo” cycle is the foundation of all backtracking algorithms.
The key insight is that every backtracking problem can be visualized as a decision tree. Each node is a partial solution. Each branch is a choice. Leaves are complete solutions (or dead ends). Backtracking is just DFS on this decision tree.
What We’ll Cover:
Where This Appears in Interviews:
| Company Type | How This Section Appears |
|---|---|
| FAANG / Big Tech | Subsets, Permutations, and Word Search are top-30 most-asked problems |
| Mid-size companies | Combination Sum and basic subset generation appear in phone screens |
| Startups | Constraint satisfaction and search problems that reduce to backtracking |
| Online assessments | Subsets and permutations are common since they test recursive thinking |
Core Idea for This Section: Backtracking explores a decision tree and abandons paths that cannot lead to valid answers. Every backtracking problem follows the same structure: choose, explore, unchoose.
Every Backtracking Problem Is a Tree:
At each level of the tree, you make one decision. The branches represent the available choices. As you go deeper, the partial solution grows. When you reach a leaf (no more decisions to make), you have a complete candidate. If the candidate is valid, record it. Then backtrack and try the next branch.