Backtracking Problems for Coding Interviews
Backtracking builds a solution step by step and undoes a choice when it cannot lead to an answer. It is the tool for permutations, combinations, subsets, board puzzles and constraint problems, where you must explore many possibilities.
The template is choose, explore, un-choose. The skill is pruning: stop early when a partial solution already breaks a rule, and avoid repeated work by sorting or skipping duplicates. Practise estimating the size of the search space.
Coding problems
- Coin Change Ways (Advanced)
- Max Chunks to Make Sorted (Advanced)
- Permutation Inside a String (Intermediate)
- Count Subsets With Given XOR (Intermediate)
- Coin Change Ways (Intermediate)
- Partition Equal Subset Sum (Intermediate)
- Next Permutation (Intermediate)
- Largest Divisible Subset Size (Advanced)
- Minimum Operations to Sort by Reversals (Intermediate)
- Count Subarrays With Median K (Basic)
- Exact Cover Solver (Advanced)
- Linked List Components (Intermediate)
- Ones and Zeroes (Intermediate)
- Combination Sum IV (Intermediate)
- Count Vowel Permutations (Intermediate)
- Profitable Schemes (Advanced)
- Ways to Reorder Array to Get Same BST (Advanced)
- Validate Stack Sequences (Intermediate)
- Prime Arrangements (Basic)
- Permutations of Distinct Values (Intermediate)
- Combination Sum (Intermediate)
- Combination Sum II (Intermediate)
- Combination Sum III (Intermediate)
- N-Queens Count (Advanced)
- Letter Combinations of a Phone Number (Intermediate)
- Beautiful Arrangement (Intermediate)
- Letter Case Permutation (Basic)
- Combinations (Intermediate)
- Permutation Rank (Advanced)
- Couples Holding Hands (Advanced)
- Count Subsets With Sum K (Intermediate)
- Maximum Height by Stacking Cuboids (Advanced)
- K Inverse Pairs Array (Advanced)
- Count Increasing Quadruplets (Advanced)
- Derangements Count (Basic)
- DI String Valid Permutations (Advanced)
- Palindrome Permutation Check (Basic)
- Build Array From Permutation (Basic)
- Prefix Common Array (Basic)
- Decode XORed Permutation (Advanced)
- Largest Combination With Positive AND (Intermediate)
- Subset Sum Count to Target (Intermediate)
- Minimum Difference Between Two Subset Sums (Intermediate)
- Smallest Unreachable Subset Sum (Intermediate)
- Collecting Numbers Rounds (Basic)
- Minimum Cost to Make Array a Permutation (Basic)
- Expected Inversions in Random Permutation (Basic)
- Expected Cycles in Random Permutation (Intermediate)
- Maximum Subset XOR (Advanced)
- Number of Distinct Subset XOR Values (Advanced)
- Count Subsets With Zero AND (Advanced)
- Next Lexicographic Permutation of String (Intermediate)
- Stack Sortable Permutation Check (Intermediate)
- Unsigned Stirling Number of the First Kind (Intermediate)
- Eulerian Number Modulo (Advanced)
- Involution Count Modulo (Intermediate)
- Closest Subset Sum to Target Meet in the Middle (Advanced)
- Count Subarrays That Are Permutations (Intermediate)
- Lehmer Code of Permutation (Intermediate)
- Permutation From Lehmer Code (Intermediate)
Related
- Sliding window problems
- Dynamic programming problems
- Graph problems
- Practice coding problems online
- All interview question topics
Frequently asked questions
How do I get good at backtracking problems?
Solve a few problems of each difficulty, name the pattern after you solve each one, and revisit the ones you needed hints for a week later. The list on this page is ordered so you can start with the basic ones.
Can I run backtracking problems online here?
Yes. Every problem opens in an editor that runs your code against test cases in Java, Python, C++, Go, JavaScript and six more languages, with no setup.