Quant Memo
Core

Catalan Numbers

A single sequence of integers that counts valid parenthesis matchings, binary tree shapes, and random walks that never dip below zero — showing up wherever a process must stay balanced from start to finish.

Prerequisites: Permutations and Combinations, Recurrence Relations

A market maker's inventory over a trading day goes up on buys and down on sells, and by close it must return to flat — but it can never go net short of the position limit in between. How many distinct valid sequences of ups and downs achieve this? Naive counting overcounts wildly, because most random sequences dip below the limit at some point and have to be thrown out. There's a single, elegant integer sequence that counts exactly this kind of "balanced, never-goes-negative" path — the Catalan numbers — and it counts several unrelated-looking problems too.

An analogy: matched parentheses

Think of a string of open and close parentheses, like (()()). It's "valid" if, reading left to right, you never have more closes than opens so far, and you end with them equal. (() is invalid — it never closes. The number of valid arrangements of nn pairs is the nn-th Catalan number. A market maker's flat-to-flat, never-below-limit inventory path is the same structure: "open" is a buy, "close" is a sell, and staying non-negative is staying above the limit.

The formula, one piece at a time

Let CnC_n denote the nn-th Catalan number, counting balanced sequences made of nn "up" moves and nn "down" moves that never go negative. It has a closed form:

Cn=1n+1(2nn)=(2n)!(n+1)!n!.C_n = \frac{1}{n+1}\binom{2n}{n} = \frac{(2n)!}{(n+1)!\,n!} .

In words: take the number of ways to arrange nn ups and nn downs in any order — that's (2nn)\binom{2n}{n}, "2n2n choose nn" — and divide by n+1n+1. That division is the correction removing every arrangement that dips negative; it falls out of a clever mapping (the reflection principle) that pairs each "bad" path with a unique reflected path in the full, unconstrained count. CnC_n also satisfies a recurrence built from splitting a path at its first return to zero:

Cn+1=i=0nCiCni,C0=1.C_{n+1} = \sum_{i=0}^{n} C_i \, C_{n-i}, \qquad C_0 = 1 .

In words: a balanced sequence of length n+1n+1 either returns to zero for the first time after some prefix of length ii, followed by a balanced sequence of the remaining nin-i — summing over every possible first-return point ii builds every valid sequence exactly once.

Worked example 1: counting order sequences by hand

For n=3n = 3 (3 buys, 3 sells, inventory never negative): C3=14(63)=14×20=5C_3 = \frac{1}{4}\binom{6}{3} = \frac{1}{4} \times 20 = 5. Listing them confirms it: with B for buy (+1) and S for sell (−1), the 5 valid sequences are BBBSSS, BBSBSS, BBSSBS, BSBBSS, BSBSBS. Every other arrangement of three B's and three S's (there are (63)=20\binom{6}{3}=20 total) dips the running total below zero at some point and is excluded.

Worked example 2: binary tree shapes

Catalan numbers also count the number of distinct shapes of a binary tree with nn internal nodes — relevant when counting ways to fully parenthesize a chain of n+1n+1 matrix multiplications (each parenthesization is one tree shape, and picking the cheapest is the matrix-chain optimization problem). For n=4n = 4: C4=15(84)=14C_4 = \frac{1}{5}\binom{8}{4} = 14 — 14 distinct ways to parenthesize a product of 5 matrices, each a different tree shape and, in general, a different total cost.

n C₁=1 C₂=2 C₃=5 C₄=14 C₅=42
Catalan numbers grow quickly but stay a modest fraction of the full unrestricted count $\binom{2n}{n}$ — the non-negativity constraint prunes an ever-larger share of arrangements as n grows.
step number valid: never below start excluded: dips below start
Both paths return to the same start and end point, but only the one that never dips below its starting level counts toward the Catalan number — the reflection principle counts and removes exactly the dipping paths.

What this means in practice

Catalan numbers appear anywhere something must stay "balanced" or non-negative while returning to its starting state: matched brackets in a parser, ways to triangulate a polygon, and counting inventory paths that respect a risk limit. Recognizing a "balanced path, never negative, ends at zero" structure is the trigger to reach for 1n+1(2nn)\frac{1}{n+1}\binom{2n}{n} instead of brute-force enumeration.

The nn-th Catalan number, Cn=1n+1(2nn)C_n = \frac{1}{n+1}\binom{2n}{n}, counts sequences of nn up-moves and nn down-moves that never go negative — the "1n+1\frac{1}{n+1}" correction removes exactly the paths that dip below the starting level, out of all (2nn)\binom{2n}{n} unrestricted arrangements.

The common mistake is forgetting the non-negativity constraint entirely and just computing (2nn)\binom{2n}{n} — the count of all arrangements of nn ups and nn downs, balanced or not. That number is always much larger than CnC_n (for n=3n=3, (63)=20\binom{6}{3}=20 versus C3=5C_3=5) and answers a different question: "how many ways can inventory end flat," not "how many ways can it end flat while never breaching the limit." Confirm which question is actually being asked before reaching for the formula.

Related concepts

Practice in interviews

Further reading

  • Stanley, Catalan Numbers
  • Feller, An Introduction to Probability Theory and Its Applications, vol. 1, ch. 3
ShareTwitterLinkedIn