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

  1. Add Up the Digits (Easy): Given a number, return the sum of its digits, using recursion.
  2. Power by Halving (Easy): Given a base and an exponent, return base to that power using O(log n) multiplications.
  3. 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.
  4. 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.
  5. Every Capitalisation (Medium): Given a string, return every version of it with each letter in lowercase or uppercase.
  6. Every Subset (Medium): Given a list, return every subset of it, including the empty one.
  7. 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.
  8. No Two Ones Together (Medium): Given n, return every binary string of length n with no two 1s next to each other.
  9. Pack the Loadout (Medium): Each piece of gear has a weight and a power.
  10. 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.
  11. Towers of Hanoi (Medium): Given n disks on peg A, return the moves, like "A->C", that move them all to peg C.
  12. 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.
  13. Cut Into Mirrors (Hard): Given a string, return how many ways you can cut it into pieces that are all palindromes.
  14. 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.
  15. 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.
  16. Every Order (Hard): Given a list, return every possible ordering of it.
  17. 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.
  18. Letters on the Keypad (Hard): Given a string of phone keypad digits from 2 to 9, return every letter combination they could spell.
  19. Merge Sort (Hard): Given an array, sort it with merge sort.
  20. 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.
  21. 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.
  22. Rebuild the Address (Hard): Given a string of digits, return every valid IP address you can make by adding three dots.
  23. Split It Evenly (Hard): Given a list of numbers, return true if you can split them into two groups with equal sums.
  24. 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