Pattern

Recursion

Solve a problem by expressing it in terms of a smaller version of itself, then trust the smaller call to be correct.

What it is

Recursion solves a problem by identifying a smaller subproblem of the same shape, calling yourself on it, and combining that result with a small amount of local work. It underlies several different-looking techniques here: exponentiation by repeated halving, tree construction from paired traversals, exhaustive search with save-and-restore state (backtracking), and parsers that handle nested structure by recursing into each nested group. What ties them together is the recursive leap of faith — assume the recursive call on the smaller input already works, and reason only about how to build the current answer from that result.

When it applies

Tells in the problem

The classic mistake

The most common bug is calling the recursive function twice on the same subproblem instead of once and reusing the result — writing f(n // 2) * f(n // 2) instead of computing half = f(n // 2) and then half * half — which silently turns an O(log n) algorithm into an O(n) one while still producing the correct answer, so it passes correctness tests and only fails on a timing or scale check. The second common mistake is choosing recursion for a linear (not halving) reduction on an input large enough that the call stack itself becomes the limiting resource — a list or string with tens of thousands of elements recursed one at a time will exhaust a typical default stack depth long before it exhausts any reasonable time budget. Before committing to a recursive solution, ask whether each call meaningfully shrinks the problem (by a constant fraction, ideally) or only shrinks it by one, and check the input size against your language's practical recursion depth.

Questions to start with

All 6 recursion questions

Problem statement, examples and hints are free on every page; the worked approach, solution and tests are in the company guides.

Free starter pack

13 must-tier questions with full solutions

Sign in with an email — no purchase — and read them in the full reader.

No password, no spam. One sign-in email; you can read the pack in the full reader.