What is Backtracking?
Backtracking is a systematic way to explore all possible solutions by building candidates incrementally and abandoning a path (“backtracking”) as soon as it violates constraints.Quick Recognition: If you need to generate all possibilities, find all valid combinations, or solve constraint satisfaction problems, think Backtracking!
Pattern Recognition Checklist
Use Backtracking When
- Need all combinations/permutations
- Problem is about constraint satisfaction
- Generate all valid solutions
- Need to explore decision tree
- Pruning can reduce search space
Don't Use When
- Only need one solution (might use simpler approach)
- Optimal solution needed (use DP instead)
- Counting only (DP often better)
- No way to prune invalid paths
When to Use
Combinations/Permutations
Generate all arrangements or selections
Subsets
Generate all possible subsets
Constraint Satisfaction
Sudoku, N-Queens, crosswords
Path Finding
Find all paths, word search
Core Template
Pattern Variations
1. Subsets (All Combinations)
2. Permutations
3. Combinations (Choose K)
4. N-Queens
Classic Problems
1. Subsets
1. Subsets
Pattern: Include or exclude each elementKey: Every path is a valid subset
2. Permutations
2. Permutations
Pattern: Use boolean array to track used elementsKey: All positions are valid choices
3. Combination Sum
3. Combination Sum
Pattern: Subsets with target sum, elements reusableKey: Start from same index (not i+1) for reuse
4. Word Search
4. Word Search
Pattern: Grid DFS with backtrackingKey: Mark visited, restore after exploration
Common Mistakes
The Backtracking Template
Every backtracking solution follows this structure:Problem Type Quick Reference
| Problem Type | Include/Exclude | Start Index | Reuse Elements |
|---|---|---|---|
| Subsets | Every node is result | i + 1 | No |
| Combinations | Leaf nodes only | i + 1 | No |
| Combination Sum | Leaf nodes only | i (same) | Yes |
| Permutations | Leaf nodes only | 0 (use visited) | No |
| Unique Results | Skip duplicates at same level | i + 1 | No |
Interview Problems by Company
- Medium
- Hard
| Problem | Company | Key Concept |
|---|---|---|
| Subsets | All FAANG | Basic include/exclude |
| Permutations | All FAANG | Used array tracking |
| Combination Sum | Amazon, Meta | Reusable elements |
| Letter Combinations | Meta | Phone keypad mapping |
| Word Search | Amazon, Meta | Grid backtracking |
Interview Tips
How to Explain Your Approach
How to Explain Your Approach
Script for interviews:
- “This requires generating all possibilities, so I’ll use backtracking.”
- “I’ll build solutions incrementally, making one choice at a time.”
- “At each step, I’ll check if the current path is valid.”
- “After exploring a choice, I’ll undo it (backtrack) and try the next.”
- “I’ll prune invalid paths early to optimize.”
When Interviewer Says...
When Interviewer Says...
| Interviewer Says | You Should Think |
|---|---|
| ”Generate all combinations” | Backtracking |
| ”Find all permutations” | Backtracking with used array |
| ”All valid configurations” | Backtracking with constraints |
| ”Can you prune?” | Early termination conditions |
| ”Handle duplicates” | Sort + skip same at same level |
Avoiding Duplicates
Avoiding Duplicates
When input has duplicates but you need unique results:
- Sort the input first
- Skip duplicates at same level:
if i > start and nums[i] == nums[i-1]: continue - This ensures same value isn’t used twice at same position in recursion tree
Practice Problems
| Problem | Difficulty | Link |
|---|---|---|
| Subsets | Medium | LeetCode 78 |
| Permutations | Medium | LeetCode 46 |
| Combination Sum | Medium | LeetCode 39 |
| N-Queens | Hard | LeetCode 51 |
| Word Search | Medium | LeetCode 79 |
Practice Roadmap
1
Day 1: Subsets
- Solve: Subsets, Subsets II
- Focus: Include/exclude pattern
2
Day 2: Combinations
- Solve: Combinations, Combination Sum
- Focus: Target constraints
3
Day 3: Permutations
- Solve: Permutations, Permutations II
- Focus: Used array tracking
4
Day 4: Grid Problems
- Solve: Word Search, N-Queens
- Focus: 2D backtracking