Turning a Word Puzzle Into a Recurrence
Most 'count the ways to...' interview puzzles are recurrences wearing a costume. The skill is translating a story into an equation that relates the answer for n to the answer for smaller n — after that, it's mechanical.
Prerequisites: How to Crack a What-Comes-Next Question
A staircase has steps. You can climb it taking either 1 or 2 steps at a time. How many distinct ways are there to climb the whole staircase?
Try small cases by hand before reading on — the goal isn't the final formula, it's noticing the mechanical rule that turns the story into an equation.
The translation move
A recurrence relates the answer for a given size to the answer for smaller sizes. To find it, don't think about the whole staircase at once — condition on the very last move. Ask: "how did I arrive at step ?" There are exactly two ways to have just landed on step : you took a single step from step , or you took a double step from step . Every valid climb ends in exactly one of these two ways, and the two cases don't overlap.
So the number of ways to reach step , call it , is the number of ways to have reached step (then take one more single step) plus the number of ways to have reached step (then take one double step):
That's a recurrence — the Fibonacci recurrence, in fact, though the staircase story gives no hint of that until you derive it.
Solving the staircase
Base cases first — the recurrence needs somewhere to bottom out. (only one way: a single step). (either two singles, or one double). Now grind forward:
So for a 5-step staircase, there are 8 distinct ways to climb it. Sanity check by hand: 1-1-1, 1-2, 2-1 — exactly three, matching.
To find a recurrence, condition on the last thing that happens (the final step, the final card, the final move) and count how many ways each choice of "last thing" could have occurred, in terms of smaller versions of the same problem. That sum, over all valid last moves, is the recurrence.
A second example: tiling a 2×n board
How many ways can you tile a board using dominoes (each domino covers exactly two adjacent squares, placed either horizontally or vertically)?
Condition on how the rightmost column is covered. Two cases: either it's covered by a single vertical domino (leaving a board to tile the rest of, contributing ways), or the rightmost two columns are covered together by two horizontal dominoes stacked on top of each other (leaving a board, contributing ways) — no other way to cover the rightmost column is possible without leaving a gap. So:
Same recurrence shape as the staircase — not a coincidence, since both are "count the ways, conditioning on the last unit of structure, where the last unit is either size 1 or size 2." Base cases: (one vertical domino), (two verticals, or two horizontals stacked). Then , , . A board has 8 tilings — the identical numbers as the staircase, because it's the identical recurrence with identical base cases.
If two puzzles produce the same recurrence and the same base cases, they have the same answer sequence, even if the stories look nothing alike. Recognizing "this is Fibonacci again" or "this is the same shape as a problem I've already solved" saves you from re-deriving a formula from scratch.
Generalizing past the last move
Not every recurrence conditions on the immediately preceding unit — sometimes you need to condition on where a particular element sits ("where is the largest number placed") or split into more than two cases (a staircase allowing 1, 2, or 3 steps gives , the "tribonacci" recurrence). The reusable skill, though, is always the same first move: identify the very last decision that produces a size- object, enumerate the finitely many ways that decision could have gone, and express the count for each way in terms of a strictly smaller version of the same problem. That translation is 90% of solving any "count the ways" interview question — the arithmetic that follows is the easy part.
Related concepts
Practice in interviews
Further reading
- Graham, Knuth & Patashnik, Concrete Mathematics (ch. 1 & 7)
- Engel, Problem-Solving Strategies (ch. 1)