Generating Functions as a Puzzle Tool
A generating function packs an entire infinite sequence into one polynomial-like expression, where the coefficient of x^n is the n-th answer. Counting problems that look hard as combinatorics often become easy algebra once repackaged this way.
Prerequisites: Turning a Word Puzzle Into a Recurrence
In how many ways can you make $1 (100 cents) using pennies (1¢), nickels (5¢), and dimes (10¢)? Order doesn't matter — 3 dimes and 1 nickel and 65 pennies is one way, however you list the coins.
You could set up nested loops over how many dimes, then how many nickels, then check the pennies fill the rest — and that works, but there's a slicker packaging that turns the whole counting problem into multiplying three simple expressions together.
The packaging trick
For a single denomination — say pennies — the number of ways to contribute exactly cents using only pennies is 1 for every (you either use pennies or you don't; there's exactly one way). Encode that entire infinite list of answers (1, 1, 1, 1, ...) as the coefficients of a formal power series:
Read as "a placeholder marking cents contributed," and the coefficient in front of it as "the number of ways to contribute that amount using this denomination." For nickels, you can only contribute in multiples of 5, so only every fifth power of has a nonzero coefficient:
And for dimes: .
Here's the payoff. If you multiply , the coefficient of in the product is exactly the number of ways to pick a penny-count, a nickel-count, and a dime-count that together sum to 100 — because multiplying power series adds exponents in every possible combination, which is precisely what "choose some pennies AND some nickels AND some dimes that sum to 100" means. The hard combinatorics question has become a purely mechanical algebra question: find one coefficient in a product of three series.
Why multiplying series does the counting for you
This deserves a small concrete check before trusting it on the full $1 problem. Suppose you only had pennies and nickels, and wanted ways to make 6 cents. and . Multiply and collect the term: it can come from ( term of ) × ( term of ) — one way, using 6 pennies and 0 nickels — or ( term of ) × ( term of ) — one way, 1 penny and 1 nickel. No other pairing of exponents sums to 6. Total: 2 ways, which matches direct enumeration (6 pennies; or 1 nickel + 1 penny). The algebra reproduced exactly what hand-counting gives, confirming the machinery is doing what it claims.
A generating function turns "count the combinations that sum to " into "find a coefficient in a product of series." Each factor encodes one ingredient's possible contributions; multiplying factors automatically enumerates every way to combine ingredients, because multiplying series adds exponents across every pairing.
Solving the $1 puzzle
Rather than expanding the full product symbolically, use the same direct-counting logic the generating function represents, now that you trust it: for each number of dimes , the dimes contribute cents, leaving cents for nickels and pennies. For a fixed remainder , the number of ways to split between nickels and pennies is the number of nickel-counts from 0 up to (pennies fill in the rest automatically) — that's ways.
Since is always a multiple of 10, , giving ways for each . Summing over to :
121 ways to make a dollar out of pennies, nickels, and dimes. The generating function justified why this "fix the dimes, then count nickel-penny splits" approach is exhaustive and non-overlapping — every term in the triple product corresponds to exactly one (pennies, nickels, dimes) triple, so summing over all valid dime-counts covers every case exactly once.
You rarely need to expand a generating function by hand in an interview. The value of setting one up is structural: it proves your case-by-case counting method is complete (no missed or double-counted combinations) before you grind through the sum.
Where this generalizes
Generating functions are the standard tool behind partition-counting puzzles ("how many ways to write as a sum of..."), and the same "encode a sequence as coefficients, manipulate the whole object algebraically" trick solves linear recurrences in closed form (multiply the recurrence by , sum over , and the generating function satisfies a simple algebraic equation you can solve for a closed form). In a quant context, the identical idea underlies option pricing via characteristic functions and moment-generating functions in probability — packing an entire distribution into one transformed object so that an operation that's hard on the distribution (like a convolution, i.e. summing independent variables) becomes easy on the transform (like a product).
Related concepts
Practice in interviews
Further reading
- Wilf, generatingfunctionology (freely available, ch. 1)
- Graham, Knuth & Patashnik, Concrete Mathematics (ch. 7)