Condition on the last step
When a direct formula is not obvious, let be the answer for size and ask: how can a valid object of size end? Each way of ending leaves a smaller valid object, and the cases must be disjoint and exhaustive. That gives a recurrence; small cases give the initial conditions.
Tilings. Tile a strip with squares and dominoes. The last tile is a square (leaving ) or a domino (leaving ), so with . These are Fibonacci numbers: with . The same recurrence counts domino tilings of a board (the last column is one vertical domino, or the last two columns are two horizontal ones) and ways to climb stairs taking 1 or 2 steps at a time.
Binary strings with no two consecutive 1s. A valid string of length ends in 0 (anything valid of length before it) or in 01 (anything valid of length before it). So with , , giving : , and .
The common mistake is cases that overlap or miss something. "Ends in 0" and "ends in 1" are exhaustive, but "ends in 1" is not a free choice, which is why you expand it to "ends in 01".
Worked example
no two heads in a row
You flip a fair coin 10 times. What is the probability that no two consecutive flips are both heads? How fast does this probability decay with ?
Show solutionHide solution
Map H to 1 and T to 0. The good sequences are the binary strings of length 10 with no "11", and . So
For the decay rate, with , so . Each extra flip multiplies the probability by about 0.81, not by the naive you might guess from "each new pair is HH with probability ". The pairs overlap, so they are not independent.
A state-based version of the same count: let , be the number of good strings ending in 0 and in 1. Then and , which is the code below.
Recurrences in code
The state version is the safest way to code these counts, and it generalises to any "forbidden pattern" problem: the state is how much of the pattern you have just seen.
from functools import lru_cache
def no_consecutive_ones(n: int) -> int:
# end0, end1: valid strings of the current length ending in 0 / in 1
end0, end1 = 1, 1 # length 1: "0", "1"
for _ in range(n - 1):
end0, end1 = end0 + end1, end0
return end0 + end1
@lru_cache(maxsize=None)
def catalan(n: int) -> int:
# first-return decomposition: C_n = sum_i C_i * C_{n-1-i}
if n == 0:
return 1
return sum(catalan(i) * catalan(n - 1 - i) for i in range(n))
print([no_consecutive_ones(n) for n in range(1, 11)])
print([catalan(n) for n in range(10)])
Output:
[2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
[1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862]
Catalan numbers and Dyck paths
A Dyck path of length is a sequence of up-steps and down-steps whose running sum never goes below 0. Balanced strings of pairs of brackets, ways to fully parenthesise a product of factors, and triangulations of a convex -gon are all in bijection with them. Their count is the Catalan number .
Recurrence (first return). A non-empty Dyck path first returns to 0 at step for some . Before that it is "up, a Dyck path of length , down"; after it, any Dyck path of length . So
Closed form (reflection). Of the paths from 0 to 0, call a path bad if it ever reaches . Reflect a bad path after its first visit to (swap every later up and down). It now ends at , and every path to arises exactly once this way. Paths to have ups and downs, so
The sequence is . Recognising in small cases is often how you spot a Catalan problem in an interview.
The ballot theorem
In an election, A gets votes and B gets , counted in a uniformly random order. The probability that A is strictly ahead throughout the count is
Proof by reflection: the first vote must be for A, so count paths from height 1 at step 1 to height at step that never touch 0. All such paths number . Those that do touch 0 reflect (up to the first touch) into paths starting at , of which there are . Dividing the difference by gives .
Worked example
a 6–4 election
A wins an election 6 votes to 4. The ballots are counted in a random order. What is the probability that A is strictly ahead after every ballot? Check it by counting paths.
Show solutionHide solution
The ballot theorem gives .
By counting: there are orders. The first ballot must be for A. From there, paths from height 1 to height 2 over the remaining 9 ballots number (5 more A votes among 9). The ones that touch 0 reflect to paths from to , which need 6 ups among 9: . So the good count is , and .
A quick interview corollary: with , , the probability is . Multiplying by gives , which is another way to see the Catalan formula.
Solving linear recurrences
A homogeneous linear recurrence with constant coefficients,
is solved by trying . This gives the characteristic equation .
- Distinct roots : , with the fixed by the initial values.
- A root of multiplicity contributes .
- Inhomogeneous : add one particular solution to the general homogeneous solution. For a polynomial , try a polynomial of the same degree, raised by one degree for each time 1 is a root of the characteristic equation.
Fibonacci: has roots , and give Binet's formula . Since , is the nearest integer to .
The asymptotic growth is set by the largest root, which is often all an interviewer wants: "the count grows like ".
Worked example
two recurrences solved
(a) Solve with , .
(b) straight lines are drawn in the plane, no two parallel and no three through one point. Find the number of regions .
Show solutionHide solution
(a) The characteristic equation is , so and . From : . From : , so and
Check: , and .
(b) Condition on the last line. It crosses each of the other lines exactly once, at distinct points, so it passes through existing regions and splits each in two:
The characteristic root is 1, so for try a quadratic. Summing directly is easier: . So 10 lines give 56 regions (the pizza-cutting puzzle), and 6 give 22.
Knowledge check
In how many ways can a 2 × 8 board be tiled with 1 × 2 dominoes?
Knowledge check
How many sequences of 5 opening and 5 closing brackets are balanced (every prefix has at least as many openings as closings)?
Knowledge check
Solve with , .
Key takeaways
- Set up a recurrence by asking how a valid object of size can end (or start). Make the cases disjoint and exhaustive.
- Tilings with pieces of size 1 and 2, stair climbing, and binary strings with no "11" are all Fibonacci; count with states when the constraint is a forbidden pattern.
- Catalan numbers count Dyck paths and balanced brackets; derive them by first return (recurrence) or by reflection (closed form).
- Ballot theorem: A strictly ahead throughout with probability .
- Linear recurrences: roots of the characteristic equation, terms for repeated roots, plus a particular solution for an inhomogeneous term. The largest root sets the growth rate.
Practice problems
Finished reading?
Answer the 3 knowledge checks to complete this lesson (0 done).