Quant Memo
Foundational

The Indicator-Variable Trick

Rewriting a hard counting problem as a sum of simple 0/1 random variables so linearity of expectation solves it in one line, without ever computing the full distribution.

Prerequisites: Conditional Probability

The problem. In a shuffled deck of 52 cards, how many cards, on average, are in their original "sorted" position (card 1 in slot 1, card 2 in slot 2, and so on)? The distribution of that count is genuinely ugly — computing it directly means wading through inclusion-exclusion over all 52 positions. The expected value, though, is one line, if you know the trick.

The trick: split a hard sum into simple 0/1 pieces

Instead of thinking about "the number of matches" as one complicated random variable, define 52 tiny ones: let Xi=1X_i = 1 if card ii lands in position ii, and Xi=0X_i = 0 otherwise. The total number of matches is just their sum, X=X1+X2++X52X = X_1 + X_2 + \cdots + X_{52}. This looks like it hasn't helped — XX is exactly as hard to describe as before — but linearity of expectation says E[X]=E[X1]+E[X2]++E[X52]E[X] = E[X_1] + E[X_2] + \cdots + E[X_{52}] regardless of whether the XiX_i are independent. That "regardless of independence" clause is the entire trick: you never have to worry about how one card's position affects another's, because expectation of a sum is always the sum of expectations.

Each XiX_i is an indicator variable, and for an indicator, E[Xi]=P(Xi=1)E[X_i] = P(X_i = 1) — the expectation of a 0/1 variable is just the probability it equals 1. For a uniformly random shuffle, P(card i in position i)=152P(\text{card } i \text{ in position } i) = \frac{1}{52} for every ii, by symmetry. So:

E[X]=i=152152=52×152=1.E[X] = \sum_{i=1}^{52} \frac{1}{52} = 52 \times \frac{1}{52} = 1.

In plain English: on average, exactly one card ends up in its original spot, no matter how the deck is shuffled — a strikingly clean answer to a question whose full distribution is genuinely messy.

Worked example: expected number of fixed points in general

The argument never used the number 52 in an essential way. For nn cards shuffled uniformly at random, define Xi=1X_i = 1 if card ii is in position ii. Then P(Xi=1)=1nP(X_i = 1) = \frac{1}{n} by the same symmetry argument, so E[X]=i=1n1n=n×1n=1E[X] = \sum_{i=1}^n \frac{1}{n} = n \times \frac{1}{n} = 1 — the expected number of matches is exactly 1 for any deck size nn, from a 2-card deck to a million-card one. That the answer doesn't depend on nn is itself the tell that indicator variables were the right tool: a direct combinatorial approach would need nn-dependent inclusion-exclusion terms that all happen to cancel down to this constant.

P(match) 1/n 1/n 1/n 1/n 1/n card 1 card 2 card 3 ... card n sum = n × (1/n) = 1
Each card contributes a small 1/n chance of being a fixed point; linearity of expectation sums these n small probabilities into a clean expected count of 1, regardless of how the cards depend on one another.

The general technique: when to reach for indicators

Whenever a question asks for the expected count of something — matches, collisions, empty bins, fixed points, inversions — and the something can be described as "does event ii happen," define one indicator per potential occurrence, sum them, and apply linearity. You never need independence, you never need the joint distribution, and you rarely need more than a one-line probability calculation per indicator. This is the single most reusable trick in probability interviews precisely because it converts "count something complicated" into "add up simple probabilities."

To find the expected count of anything, write it as a sum of 0/1 indicator variables, one per possible occurrence, and apply linearity of expectation: E[Xi]=E[Xi]=P(Xi=1)E[\sum X_i] = \sum E[X_i] = \sum P(X_i=1). This works even when the events are dependent, which is what makes it so powerful.

If a problem asks "how many on average," reach for indicators before reaching for the full distribution — you almost never need the distribution to answer an expectation question.

Related concepts

Practice in interviews

Further reading

  • Grinstead & Snell, Introduction to Probability, ch. 6
ShareTwitterLinkedIn