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 pairs is the -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 denote the -th Catalan number, counting balanced sequences made of "up" moves and "down" moves that never go negative. It has a closed form:
In words: take the number of ways to arrange ups and downs in any order — that's , " choose " — and divide by . 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. also satisfies a recurrence built from splitting a path at its first return to zero:
In words: a balanced sequence of length either returns to zero for the first time after some prefix of length , followed by a balanced sequence of the remaining — summing over every possible first-return point builds every valid sequence exactly once.
Worked example 1: counting order sequences by hand
For (3 buys, 3 sells, inventory never negative): . 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 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 internal nodes — relevant when counting ways to fully parenthesize a chain of matrix multiplications (each parenthesization is one tree shape, and picking the cheapest is the matrix-chain optimization problem). For : — 14 distinct ways to parenthesize a product of 5 matrices, each a different tree shape and, in general, a different total cost.
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 instead of brute-force enumeration.
The -th Catalan number, , counts sequences of up-moves and down-moves that never go negative — the "" correction removes exactly the paths that dip below the starting level, out of all unrestricted arrangements.
The common mistake is forgetting the non-negativity constraint entirely and just computing — the count of all arrangements of ups and downs, balanced or not. That number is always much larger than (for , versus ) 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