All puzzles › Backtracking and Recursion
Backtracking and Recursion Practice Problems
24 puzzles (2 easy, 9 medium, 13 hard), in Python, JavaScript and C++.
Backtracking builds an answer one decision at a time, and when a decision leads nowhere it undoes it and tries the next. It is how you generate every subset, every permutation, every valid arrangement, without writing a loop for each position. Recursion is the mechanism underneath: a function that solves a smaller version of its own problem.
How to spot a backtracking and recursion problem
The giveaway is a request for all of something: all combinations, all orderings, every way to place or split. Write down the choice made at each step, what makes a partial answer invalid, and what a finished answer looks like. Those three things are the whole function.
New to it? Read the lesson first: Backtracking.
The puzzles, easiest first
- Add Up the Digits (Easy): Given a number, return the sum of its digits, using recursion.
- Power by Halving (Easy): Given a base and an exponent, return base to that power using O(log n) multiplications.
- Choose k of n (Medium): Given n and k, return every way to choose k numbers from 1 to n, where order doesn't matter.
- Closest Without Going Over (Medium): Given a list of weights and a limit, return the biggest total you can make from some of them without going over the limit.
- Every Capitalisation (Medium): Given a string, return every version of it with each letter in lowercase or uppercase.
- Every Subset (Medium): Given a list, return every subset of it, including the empty one.
- Minimax Game Value (Medium): Given the scores at the bottom of a game tree and whether the first player is maximizing, return the game's value.
- No Two Ones Together (Medium): Given n, return every binary string of length n with no two 1s next to each other.
- Pack the Loadout (Medium): Each piece of gear has a weight and a power.
- Paths Across the Grid (Medium): Given a grid's rows and columns, return how many paths go from the top-left to the bottom-right moving only right or down.
- Towers of Hanoi (Medium): Given n disks on peg A, return the moves, like "A->C", that move them all to peg C.
- Add Up to the Target (Hard): Given a list of numbers and a target, return every combination that adds up to the target, where each number can be used as many times as you want.
- Cut Into Mirrors (Hard): Given a string, return how many ways you can cut it into pieces that are all palindromes.
- De Casteljau's Algorithm (Hard): Given four control points and t from 0 to 1, return the point on the Bezier curve at t, rounded to 4 decimals.
- Decode the Message (Hard): Given a string of digits where 1 means A and 26 means Z, return how many ways it can be decoded.
- Every Order (Hard): Given a list, return every possible ordering of it.
- Find the Word (Hard): Given a grid of letters and a word, return true if you can spell the word by moving up, down, left, or right, without using any cell twice.
- Letters on the Keypad (Hard): Given a string of phone keypad digits from 2 to 9, return every letter combination they could spell.
- Merge Sort (Hard): Given an array, sort it with merge sort.
- N Queens (Hard): Given n, return how many ways you can place n queens on an n by n chessboard so no two attack each other.
- Plus or Minus (Hard): Given a list of numbers and a target, return how many ways you can put a + or - in front of each number so they add up to the target.
- Rebuild the Address (Hard): Given a string of digits, return every valid IP address you can make by adding three dots.
- Split It Evenly (Hard): Given a list of numbers, return true if you can split them into two groups with equal sums.
- Well-Formed Brackets (Hard): Given n, return every balanced string of n pairs of parentheses.
Practice another pattern
Or browse by subject
Browse all 676 puzzles