Skip to content
LeetQuant
QT02 Advanced Probability and Brainteasers

QT02 · Advanced Probability and Brainteasers · Lesson 3 of 5

Urn and Card Problems

Sampling without replacement, exchangeability, Pólya's urn and the red-black card games, solved by symmetry instead of long sums.

18 min

The objective

Card and urn questions look like they need long sums over draw orders. Almost all of them collapse once you see the right symmetry. By the end of this lesson you should, for any "draw without replacement" question, ask first: which positions or objects are interchangeable, and what does that make equal?

Exchangeability: every position looks like the first

Shuffle a deck of NN cards, rr of them red. The colours read from the top form a sequence X1,…,XNX_1, \dots, X_N. The draws are dependent (seeing a red makes the next red less likely), but they are exchangeable: every rearrangement of a colour sequence has the same probability, because every ordering of the deck does.

The consequence interviewers test most:

P(Xk=red)=rNfor every k.P(X_k = \text{red}) = \frac{r}{N} \quad \text{for every } k .

Proof in one line: swapping the cards in positions 11 and kk is a bijection on shuffles that preserves probability, so position kk has the same distribution as position 11. The same swap shows that any pair of positions behaves like positions 11 and 22, so P(Xj=Xk=red)=r(r−1)N(N−1)P(X_j = X_k = \text{red}) = \frac{r(r-1)}{N(N-1)} for any j≠kj \ne k.

This is why drawing straws is fair whatever your turn, and why "the 7th card is red" is no harder than "the 1st card is red". Notice what the statement is not: it is unconditional. Once you have seen some cards, the remaining positions are exchangeable among themselves, with the updated counts.

Without replacement: counts and moments

The number of reds KK in the first nn cards is hypergeometric:

P(K=k)=(rk)(N−rn−k)(Nn).P(K = k) = \frac{\binom{r}{k}\binom{N - r}{n - k}}{\binom{N}{n}} .

Do not compute its moments from this formula. Write K=∑i=1n1[Xi=red]K = \sum_{i=1}^{n} \mathbb 1[X_i = \text{red}] and use the two exchangeability facts above. With p=r/Np = r/N,

E[K]=np,Var⁡(K)=np(1−p) N−nN−1.\mathbb E[K] = np, \qquad \operatorname{Var}(K) = np(1-p)\,\frac{N - n}{N - 1} .

The factor N−nN−1\frac{N-n}{N-1} is the finite population correction: the indicators are slightly negatively correlated, so the variance is below the binomial one, and it is 00 when n=Nn = N (you have seen every card, so K=rK = r for certain).

Gaps and positions

The Counting module showed that mm special cards cut the other N−mN - m into m+1m + 1 gaps of equal expected size, so the first special card sits at expected position N+1m+1\frac{N+1}{m+1} (10.6 for the first ace). The same argument places the jj-th special card at

E[position of the j-th special card]=j⋅N+1m+1,\mathbb E[\text{position of the } j\text{-th special card}] = j \cdot \frac{N + 1}{m + 1} ,

since jj special cards and jj gaps lie at or before it. The second ace is at 2×10.6=21.22 \times 10.6 = 21.2 on average. Reds in a 26/26 deck: the first red is at 5327≈1.96\frac{53}{27} \approx 1.96.

Worked example

the card after the first ace

Shuffle a standard deck and turn cards over until the first ace appears. Is the next card more likely to be the ace of spades or the two of clubs?

Show solution

Intuition says the two of clubs, since the ace of spades might be the first ace. Test that by building the deck in two stages, which is a standard trick for these questions.

Two of clubs. Take it out and shuffle the other 51 cards. Inserting it into one of the 52 slots uniformly at random gives a uniformly shuffled deck. Exactly one slot, the one just after the first ace of the 51-card deck, puts it right after the first ace. Probability 152\frac{1}{52}.

Ace of spades. Take it out and shuffle the other 51, which contain 3 aces. Insert it uniformly into one of 52 slots. If it lands before the first of the three aces, it becomes the first ace and the card after it is not itself. If it lands later, the first ace is the first of the three, and the ace of spades follows it only if it lands in the slot right after that ace. Again exactly one slot works: probability 152\frac{1}{52}.

So the two are equally likely, each 152\frac{1}{52}, and so is every other card. A simulation of 400,000 shuffles gives frequencies of about 0.019 for both, matching 152≈0.0192\frac{1}{52} \approx 0.0192.

The method to say out loud: "remove the card I care about, shuffle the rest, insert it at a uniform slot, and count the slots that work". It turns a conditioning nightmare into counting one slot.

Pólya's urn

An urn holds rr red and bb black balls. Each step you draw a ball, then return it together with one extra ball of the same colour. Success breeds success, so it looks as if early luck should bias everything. Three facts make these questions easy.

1. The draws are exchangeable. The probability of a particular sequence with kk reds and n−kn - k blacks is

r(r+1)⋯(r+k−1)  b(b+1)⋯(b+n−k−1)(r+b)(r+b+1)⋯(r+b+n−1),\frac{r(r+1)\cdots(r+k-1)\; b(b+1)\cdots(b+n-k-1)}{(r+b)(r+b+1)\cdots(r+b+n-1)} ,

because the numerators for red only ever increase when a red is drawn, and likewise for black, whatever the order. The denominator grows by one each step regardless. Only the counts matter, not the order.

2. So every draw has the same marginal. By exchangeability, P(draw k is red)=P(draw 1 is red)=rr+bP(\text{draw } k \text{ is red}) = P(\text{draw } 1 \text{ is red}) = \frac{r}{r+b} for every kk. The red fraction in the urn is a martingale: its expected next value equals its current value.

3. From one of each, the count is uniform. With r=b=1r = b = 1, a sequence with kk reds in nn draws has probability k! (n−k)!(n+1)!\frac{k!\,(n-k)!}{(n+1)!}, and there are (nk)\binom{n}{k} such sequences, so

P(k reds in n draws)=(nk)k! (n−k)!(n+1)!=1n+1,k=0,1,…,n.P(k \text{ reds in } n \text{ draws}) = \binom{n}{k} \frac{k!\,(n-k)!}{(n+1)!} = \frac{1}{n+1}, \qquad k = 0, 1, \dots, n .

The long-run red fraction is Uniform(0, 1) (Beta(rr, bb) in general). Each run of the urn settles on a fraction, but which one is completely random.

Conditioning keeps the structure: after a red first draw from (1,1)(1, 1), the future is just a Pólya urn started at (2,1)(2, 1), so every later draw is red with probability 23\frac23.

Worked example

Pólya's urn after ten draws

An urn starts with one red and one black ball and follows Pólya's rule. (a) What is the probability that exactly 5 of the first 10 draws are red? (b) Given the first draw is red, what is the probability the third draw is red? (c) Given the first draw is red, what is the probability that the next two are both red?

Show solution

(a) By the uniform law, each count 0,…,100, \dots, 10 has probability 111\frac{1}{11}, so the answer is 111\frac{1}{11}. Compare a fair coin, where 5 heads in 10 has probability 2521024≈0.246\frac{252}{1024} \approx 0.246: the urn spreads the count out because early draws feed back.

(b) After a red draw the urn is (2,1)(2, 1), a fresh Pólya urn. Its draws are exchangeable, so its second draw (the overall third) is red with the same probability as its first: 23\frac23. No need to condition on the second draw.

(c) Here exchangeability is not enough, because we need a joint probability. Multiply along the path: 23⋅34=12\frac{2}{3} \cdot \frac{3}{4} = \frac12. (Check with the sequence formula: P(RRR)=1⋅2⋅32⋅3⋅4=14P(RRR) = \frac{1 \cdot 2 \cdot 3}{2 \cdot 3 \cdot 4} = \frac14, divided by P(R)=12P(R) = \frac12, gives 12\frac12.)

Red-black card games and stopping

The betting version. A 26/26 deck is turned over one card at a time. At any moment before the last card you may say "stop", and you win if the next card is red. Can timing beat 12\frac12?

No. Whenever you stop, the next card is red with probability equal to the current red fraction of the remaining deck, and by exchangeability of the remaining cards that is the same as the probability that the last card is red. So every strategy wins with the probability that the last card of the deck is red: 12\frac12. Equivalently, the red fraction is a martingale and no stopping rule changes its expected value. When an interviewer asks you to "find the optimal strategy" here, the answer is that none is better than any other, and the argument is the point.

The scoring version. Now you score +1+1 for each red turned and −1-1 for each black, and you may stop at any time and keep your score. You can always guarantee 00 (play to the end), and stopping when ahead can lock in gains. Let V(r,b)V(r, b) be the value of continuing optimally with rr reds and bb blacks left. Then V(r,0)=rV(r, 0) = r, V(0,b)=0V(0, b) = 0, and

V(r,b)=max⁡(0,  rr+b(1+V(r−1,b))+br+b(−1+V(r,b−1))).V(r, b) = \max\Big(0,\; \tfrac{r}{r+b}\big(1 + V(r-1, b)\big) + \tfrac{b}{r+b}\big(-1 + V(r, b-1)\big)\Big).

This is the template for every optimal stopping problem: value of stopping now versus expected value of one more step, solved backwards from the boundary.

Worked example

the scoring game with two of each

A deck has 2 red and 2 black cards. You turn cards one at a time, scoring +1+1 per red and −1-1 per black, and may stop whenever you like. What is the value of the game, and when should you stop?

Show solution

Build up from small decks. The value counts only future score, so it depends only on the cards left.

  • V(1,1)V(1, 1): red first (prob. 12\frac12) gives 1+V(0,1)=11 + V(0, 1) = 1, then stop; black first gives −1+V(1,0)=0-1 + V(1, 0) = 0. Continuing is worth 12>0\frac12 > 0, so V(1,1)=12V(1, 1) = \frac12.
  • V(2,1)V(2, 1): red gives 1+V(1,1)=321 + V(1, 1) = \frac32, black gives −1+V(2,0)=1-1 + V(2, 0) = 1. Continuing is worth 23⋅32+13⋅1=43\frac23 \cdot \frac32 + \frac13 \cdot 1 = \frac43.
  • V(1,2)V(1, 2): red gives 1+V(0,2)=11 + V(0, 2) = 1, black gives −1+V(1,1)=−12-1 + V(1, 1) = -\frac12. Continuing is worth 13−13=0\frac13 - \frac13 = 0, so V(1,2)=0V(1, 2) = 0: you are indifferent, and may as well stop.
  • V(2,2)=12(1+V(1,2))+12(−1+V(2,1))=12+16=23V(2, 2) = \frac12\big(1 + V(1, 2)\big) + \frac12\big(-1 + V(2, 1)\big) = \frac12 + \frac16 = \frac23.

So the game is worth 23\frac23. The strategy: turn the first card. If it is red you are +1+1 with (1,2)(1, 2) left, which is worth 00, so stop. If it is black, keep going: (2,1)(2, 1) is favourable.

For the full 26/26 deck the same recursion gives V≈2.62V \approx 2.62. The optimal rule never stops while level or behind, and stops when the lead is large relative to the cards left: a lead of 2 or 3 is enough with 4 to 8 cards left, about 4 with half the deck left, and about 6 with 30 or more left.

Knowledge check

A box has 10 red and 5 blue chips, drawn one at a time without replacement. What is the probability that the 7th chip drawn is red?

Knowledge check

A Pólya urn starts with one red and one black ball. What is the probability that none of the first 4 draws is red?

Knowledge check

A 26/26 deck is turned over one card at a time. You may say 'stop' at any point before the last card, and win if the next card is red. What is the best winning probability you can achieve?

Knowledge check

What is the expected position of the second ace in a shuffled 52-card deck?

Key takeaways

  • Draws without replacement are dependent but exchangeable: any position, or set of positions, has the same distribution as the first ones.
  • Get hypergeometric moments from indicators: mean npnp, variance np(1−p)N−nN−1np(1-p)\frac{N-n}{N-1}.
  • For "which card comes after X" questions, remove the card, shuffle the rest and insert it into a uniform slot.
  • Pólya's urn is exchangeable, its colour fraction is a martingale, and from (1,1)(1, 1) the red count after nn draws is uniform on {0,…,n}\{0, \dots, n\}.
  • No stopping rule beats a martingale (the betting red-black game is worth 12\frac12). When stopping does matter, write value = max(stop, continue) and solve backwards from the boundary.

Practice problems

Finished reading?

Answer the 4 knowledge checks to complete this lesson (0 done).