Keuangan Kuantitatif · Buku 18 · Karier

Interviews I: Maths, Probability and Speed

Buku ini belum tersedia dalam Bahasa Indonesia. Edisi: English, Français.

Interviews I: Maths, Probability and Speed · Karier

9Expectation and Indicators

Computing an average without ever finding the distribution: breaking a count into yes-or-no pieces, summing tail probabilities, using symmetry, and then going one step further to the variance of the count. The chapter starts with quick warm-ups and ends with problems that need one real idea you have to find yourself.

Question 9.1 ★ QR, QT, Trader, Analyst, Risk • any • junior

Two fair six-sided dice are rolled, and you keep the larger number shown. On average, what do you keep?

Answer

Answer to Question 9.1.

The expected value is 161/36≈4.47161/36 \approx 4.47.

Use the tail-sum formula: for a variable taking values 1,2,…1, 2, \dots, the mean equals ∑k≥1P(M≥k)\sum_{k \ge 1} P(M \ge k), because each outcome MM is counted once for every kk up to MM. The higher die is at least kk unless both dice are below kk, so P(M≥k)=1−(k−16)2P(M \ge k) = 1 - \bigl(\tfrac{k-1}{6}\bigr)^2. Summing over k=1,…,6k = 1, \dots, 6,

E[M]=6−0+1+4+9+16+2536=6−5536=16136.E[M] = 6 - \frac{0 + 1 + 4 + 9 + 16 + 25}{36} = 6 - \frac{55}{36} = \frac{161}{36}.

Follow-up. And with three dice? Cubes replace squares: 6−225/216=119/24≈4.966 - 225/216 = 119/24 \approx 4.96.

Follow-up. On average, how far apart are the two dice? The difference is the higher minus the lower, and the higher plus the lower is the sum of the dice, which averages 7. So the expected lower die is 7−161/367 - 161/36, and the difference averages 2⋅161/36−7=35/18≈1.942 \cdot 161/36 - 7 = 35/18 \approx 1.94.

Question 9.2 ★ QR, QT, Trader, Analyst • any • junior

You roll a fair die until a six appears, and you add up every number you rolled, including that final six. What is the expected total?

Answer

Answer to Question 9.2.

The expected total is 21.

The number of rolls NN is geometric with mean 6, and each roll has mean 7/27/2. Whether you stop after a roll depends only on the rolls so far, so Wald’s identity applies: E[total]=E[N]⋅7/2=21E[\text{total}] = E[N] \cdot 7/2 = 21. You can check this with first-step analysis. A roll that is not a six has mean 3, so

S=16⋅6+56 (3+S),S = \tfrac16 \cdot 6 + \tfrac56 \,(3 + S),

which gives S=21S = 21.

Classic wrong answer. “Five earlier rolls averaging 3.5, plus a six, makes 23.5.” The earlier rolls are known not to be sixes, so they average 3, not 3.5. Five of them plus the six gives 5⋅3+6=215 \cdot 3 + 6 = 21.

Follow-up. How many ones do you roll on average before the six? Exactly one. Ignore every roll except ones and sixes: each of those two is equally likely to come next, so the count before the first six is geometric with mean 1.

Question 9.3 ★ QR, QT, Trader, DS • market maker, hedge fund, crypto • junior

You shuffle a standard 52-card deck and lay the cards out in a row. On average, how many pairs of neighbouring cards have the same rank, such as two kings side by side?

Answer

Answer to Question 9.3.

On average there are exactly 3 such pairs.

Use linearity of expectation with indicators. There are 51 neighbouring pairs. Whatever the first card of a pair is, the second is one of the 51 other cards, and 3 of those share its rank, so each pair matches with probability 3/513/51. The total is 51⋅3/51=351 \cdot 3/51 = 3. The events overlap and are dependent, and that makes no difference: linearity needs no independence.

Follow-up. What is the variance of that count? Sum over pairs of pairs. There are 50 overlapping pairs of neighbours, each matching together with probability 351⋅250=1425\frac{3}{51}\cdot\frac{2}{50} = \frac{1}{425}. There are 1 225 disjoint pairs, each with probability 351⋅1462450\frac{3}{51}\cdot\frac{146}{2450}, where 1462450=250⋅149+4850⋅349\frac{146}{2450} = \frac{2}{50}\cdot\frac{1}{49} + \frac{48}{50}\cdot\frac{3}{49} splits on whether the second pair’s first card has the rank just used. Then Var=3+2⋅50425+2⋅1225⋅351⋅1462450−9=4817≈2.82\mathrm{Var} = 3 + 2 \cdot \frac{50}{425} + 2\cdot 1225\cdot\frac{3}{51}\cdot\frac{146}{2450} - 9 = \frac{48}{17} \approx 2.82.

Follow-up. How likely is a shuffle with no such pair? About 4.5%, by simulation. A Poisson with mean 3 predicts e−3≈5%e^{-3} \approx 5\%, which is close because the many indicators are rare and nearly independent.

Question 9.4 ★ QR, QT, Trader, Risk • any • junior

Two 52-card decks are shuffled separately. You turn over one card from each deck at the same time, and carry on through both decks. A match is a moment when the two cards are identical in rank and suit. Find the mean and variance of the number of matches.

Answer

Answer to Question 9.4.

The mean is 1 and the variance is 1, for a deck of any size above one card.

Use indicators: position ii matches with probability 1/521/52, so the mean is 52⋅1/52=152 \cdot 1/52 = 1. For the variance, write X2X^2 as the sum of the indicators plus the sum over ordered pairs of positions of IiIjI_iI_j. Positions ii and jj both match with probability 152⋅51\frac{1}{52 \cdot 51}, and there are 52⋅5152 \cdot 51 ordered pairs, so E[X2]=1+1=2E[X^2] = 1 + 1 = 2 and Var(X)=2−1=1\mathrm{Var}(X) = 2 - 1 = 1.

Classic wrong answer. Treating the positions as independent gives a variance of 52⋅152⋅5152=51/5252 \cdot \frac{1}{52}\cdot\frac{51}{52} = 51/52. The small positive covariance between positions supplies the rest.

Follow-up. What is the chance of no match at all? Very close to 1/e≈0.3681/e \approx 0.368, the fixed-point count being nearly Poisson with mean 1. This is the chance of a derangement.

Question 9.5 ★ QR, QT, Trader, Analyst, Dev • any • junior

You roll a fair die until every one of the six faces has appeared at least once. What is the expected number of rolls?

Answer

Answer to Question 9.5.

The expected number is 14.714.7 rolls.

This is the coupon collector. Split the wait into stages. When you have seen kk faces, each roll shows a new one with probability (6−k)/6(6-k)/6, so that stage lasts 6/(6−k)6/(6-k) rolls on average. Adding the stages,

6(16+15+14+13+12+1)=14.7.6\Bigl(\tfrac16 + \tfrac15 + \tfrac14 + \tfrac13 + \tfrac12 + 1\Bigr) = 14.7.

Follow-up. What is the variance? The stages are independent geometrics, each with variance (1−p)/p2(1-p)/p^2. Their sum is about 38.9938.99, a standard deviation of about 6.26.2 rolls. The tail is long because the last face takes 6 rolls on average by itself.

Follow-up. What is the expected sum of the rolls? Wald’s identity gives 14.7⋅3.5=51.4514.7 \cdot 3.5 = 51.45.

Why it matters. The cost is nln⁡nn \ln n, not nn. To see each of 1 000 instruments at least once in uniformly random samples of the data, you need about 7 500 draws, not 1 000. This is how to size a sample that must touch every bucket.

Question 9.6 ★ QR, QT, Trader, DS • any • junior

You toss a fair coin 21 times. A run is a longest possible block of equal results, so the tosses HHTHHH contain three runs. What is the expected number of runs in your 21 tosses, and what is its variance?

Answer

Answer to Question 9.6.

The mean is 11 and the variance is 5.

Runs are easier to count through changes. The number of runs is one more than the number of neighbouring tosses that differ. Each of the 20 neighbouring pairs differs with probability 1/21/2, so by indicators the mean is 1+20/2=111 + 20/2 = 11. The changes are independent, not only pairwise: given any toss, the next one differs with probability 1/21/2 whatever came before. So the number of changes is Binomial(20,1/2)(20, 1/2), with variance 20/4=520/4 = 5.

Follow-up. In 100 tosses, how many runs of exactly three heads do you expect? A run starting in the middle needs the pattern THHHT, with probability 1/321/32, and there are 96 such starting places. A run at either end needs only HHHT or THHH, with probability 1/161/16 each. The total is 96/32+2/16=3.12596/32 + 2/16 = 3.125.

Why it matters. This is the runs test for streaks in returns. For a year of 252 daily signs, the number of runs has mean 126.5126.5 and standard deviation about 7.97.9, so a count below about 111 is a two-standard-deviation sign of streaks.

Question 9.7 ★★ QR, QT, Trader • any • junior

You turn over a shuffled 52-card deck one card at a time. On average, at which position does the second ace appear?

Answer

Answer to Question 9.7.

The second ace appears at position 106/5=21.2106/5 = 21.2 on average.

Use a symmetry argument. The 4 aces split the 48 other cards into 5 gaps, as in Figure 9.1. Each of those 48 cards is equally likely to fall into any gap, because it can sit in any of the 5 places relative to the 4 aces with equal chance. So each gap holds 48/5=9.648/5 = 9.6 cards on average. The second ace comes after two gaps and two aces:

2⋅485+2=1065.2 \cdot \frac{48}{5} + 2 = \frac{106}{5}.
The four aces cut the deck into five gaps of equal expected size. The second ace sits after two gaps and the two aces themselves.
Figure 9.1. The four aces cut the deck into five gaps of equal expected size. The second ace sits after two gaps and the two aces themselves.

Classic wrong answer. 2⋅52/5=20.82 \cdot 52/5 = 20.8 forgets that the aces occupy positions too.

Follow-up. Where is the last ace on average? The same count gives the kk-th ace at position k⋅53/5k \cdot 53/5, so the fourth is at 42.442.4.

Question 9.8 ★★ QR, QT, Trader, Risk • any • junior

Three fair dice, each with faces numbered 1 to 100, are rolled. What is the exact expected value of the lowest of the three numbers?

Answer

Answer to Question 9.8.

The expected value is exactly 25.502525.5025.

Use the tail-sum formula. The lowest of the three is at least kk when all three dice are at least kk, which has probability (101−k100)3\bigl(\tfrac{101-k}{100}\bigr)^3. Summing from k=1k = 1 to 100100 and writing j=101−kj = 101 - k,

E[min⁡]=11003∑j=1100j3=11003(100⋅1012)2=50502106=25.5025,E[\min] = \frac{1}{100^3} \sum_{j=1}^{100} j^3 = \frac{1}{100^3}\Bigl(\frac{100 \cdot 101}{2}\Bigr)^2 = \frac{5050^2}{10^6} = 25.5025,

using the fact that the sum of the first nn cubes is the square of the sum of the first nn integers.

Classic wrong answer. Using the continuous rule, the lowest of three uniforms sits at a quarter of the range, gives 25. That is close, and it misses the extra 0.50250.5025 from the discrete faces. An interviewer asking for the exact value wants the sum.

Follow-up. What is the expected highest of the three? By the symmetry that sends a face ff to 101−f101 - f, it is 101−25.5025=75.4975101 - 25.5025 = 75.4975.

Question 9.9 ★★ QR, QT, QD, Dev • any • junior

You roll a fair die until some number comes up for the second time. What is the expected number of rolls, as an exact fraction? Then: keys are placed into a hash table of 1 024 buckets, each key going to a uniformly random bucket. About how many keys go in before the first collision?

Answer

Answer to Question 9.9.

For the die, the expected number of rolls is 1223/324≈3.771223/324 \approx 3.77. For the hash table, about 41 keys.

Use the tail-sum formula, E[T]=∑k≥0P(T>k)E[T] = \sum_{k \ge 0} P(T > k). The wait exceeds kk when the first kk rolls are all different, which has probability 66⋅56⋯6−k+16\frac{6}{6}\cdot\frac{5}{6}\cdots\frac{6-k+1}{6}. The terms for k=0,1,…,6k = 0, 1, \dots, 6 are

1,1,56,59,518,554,5324,1,\quad 1,\quad \tfrac56,\quad \tfrac59,\quad \tfrac{5}{18},\quad \tfrac{5}{54},\quad \tfrac{5}{324},

and their sum is 1223/3241223/324. Note that the wait is at least 2 and at most 7.

For nn buckets the same sum is the birthday problem. Its large-nn value is about πn/2+2/3\sqrt{\pi n/2} + 2/3, which for n=1024n = 1024 gives 40.840.8; the exact sum is also 40.840.8. That count includes the colliding key.

Why it matters. Collisions arrive at the square root of the table size, not at the table size. A 32-bit order identifier chosen at random has its first expected collision after about 82 000 orders, not 4 billion.

Question 9.10 ★★ QR, QT, Trader • any • junior

You draw independent random numbers, each uniform between 0 and 1, until their total exceeds 1. On average, how many numbers do you draw?

Answer

Answer to Question 9.10.

On average you draw e≈2.718e \approx 2.718 numbers.

Let NN be the number of draws. Then N>nN > n exactly when the first nn numbers sum to at most 1, and that has probability 1/n!1/n!. To see it, change variables to the running totals s1<s2<⋯<sns_1 < s_2 < \dots < s_n. The map has determinant one, so the probability is the volume of the region 0<s1<⋯<sn<10 < s_1 < \dots < s_n < 1, and that region is one of the n!n! equal orderings of nn points in the unit cube. The tail-sum formula gives

E[N]=∑n≥0P(N>n)=∑n≥01n!=e.E[N] = \sum_{n \ge 0} P(N > n) = \sum_{n \ge 0} \frac{1}{n!} = e.

Classic wrong answer. “Each number averages one half, so two numbers.” That ignores the overshoot: the total when you stop is above 1, not equal to 1.

Follow-up. What is the average total when you stop? By Wald’s identity, E[N]⋅12=e/2≈1.359E[N] \cdot \tfrac12 = e/2 \approx 1.359.

Follow-up. What is the variance of NN? E[N2]=∑n≥0(2n+1)P(N>n)=2e+e=3eE[N^2] = \sum_{n \ge 0} (2n+1)P(N>n) = 2e + e = 3e, so the variance is 3e−e2≈0.7663e - e^2 \approx 0.766.

Question 9.11 ★★ QR, QT, Risk, DS • any • junior, experienced

You draw one number from a continuous distribution and write it down. You then keep drawing independent numbers from the same distribution until one is larger than the number you wrote down. How many further draws do you need on average? What is the chance that one further draw is enough?

Answer

Answer to Question 9.11.

The expected number of further draws is infinite, and yet one further draw is enough half the time.

Let TT be the number of further draws. T>kT > k means the first number is the largest of the first k+1k+1, and by symmetry that has probability 1/(k+1)1/(k+1). So P(T=1)=1/2P(T = 1) = 1/2, and P(T≤3)=3/4P(T \le 3) = 3/4. The tail-sum formula gives the harmonic series,

E[T]=∑k≥01k+1=∞.E[T] = \sum_{k \ge 0} \frac{1}{k+1} = \infty.

The rare first number that is very large makes for very long waits, and those waits are long enough to outweigh their rarity.

Follow-up. What if you give up after at most 1 000 draws? The mean is then the harmonic sum H1000≈7.49H_{1000} \approx 7.49. Change the cap and the answer changes with it, which is the sign of a divergent mean.

Why it matters. The average wait from a sample never settles: quadruple the sample and it keeps growing. If you measure “time to recover the previous high” from a backtest with no drift, the sample mean tells you only how long the backtest was. Quote the median or a quantile instead.

Question 9.12 ★★ QR, QT, Risk • any • junior

100 distinct numbers are written in a random order and read from left to right. A number is a record if it is larger than everything before it, and the first number counts as a record. Find the mean and variance of the number of records.

Answer

Answer to Question 9.12.

The mean is the harmonic number H100≈5.19H_{100} \approx 5.19, and the variance is about 3.553.55.

Use indicators. The kk-th number is a record when it is the largest of the first kk, with probability 1/k1/k, so E[R]=∑k=11001/k≈5.187E[R] = \sum_{k=1}^{100} 1/k \approx 5.187. The large-nn form is ln⁡n+0.577\ln n + 0.577, here about 5.185.18.

For the variance you need a stronger fact: the indicators are independent. Knowing how the first k−1k-1 numbers are ordered among themselves says nothing about where the kk-th ranks among the first kk, and the record events depend only on those relative ranks. So the variance is the sum of the individual Bernoulli variances:

Var(R)=∑k=11001k(1−1k)≈5.187−1.635=3.55.\mathrm{Var}(R) = \sum_{k=1}^{100} \frac1k\Bigl(1 - \frac1k\Bigr) \approx 5.187 - 1.635 = 3.55.

Why it matters. Records in pure noise are common early and rare later: a year of 252 independent daily results has about 6.16.1 days that set a new high. So a strategy that sets six new highs in a year has shown you nothing yet.

Question 9.13 ★★ QR, QT, DS, Risk • any • junior

You roll a fair die 60 times. Let AA be the number of ones and BB the number of twos. What are the covariance and the correlation of AA and BB?

Answer

Answer to Question 9.13.

The covariance is −5/3-5/3 and the correlation is −1/5-1/5.

Write A=∑iIiA = \sum_i I_i and B=∑iJiB = \sum_i J_i, where IiI_i marks roll ii showing a one, and JiJ_i roll ii showing a two. Different rolls are independent, so only the same roll contributes to the covariance. A single roll cannot be both, so IiJi=0I_iJ_i = 0, and

Cov(Ii,Ji)=0−16⋅16=−136.\mathrm{Cov}(I_i, J_i) = 0 - \tfrac16 \cdot \tfrac16 = -\tfrac{1}{36}.

Over 60 rolls this gives −60/36=−5/3-60/36 = -5/3. Each count has variance 60⋅16⋅56=25/360 \cdot \tfrac16 \cdot \tfrac56 = 25/3, so the correlation is (−5/3)/(25/3)=−1/5(-5/3)/(25/3) = -1/5, whatever the number of rolls.

Follow-up. Check it another way. A+BA + B counts rolls showing a one or a two, which is Binomial(60,1/3)(60, 1/3) with variance 40/340/3. And indeed 25/3+25/3−2⋅5/3=40/325/3 + 25/3 - 2\cdot 5/3 = 40/3.

Why it matters. Counts in buckets of a fixed total are negatively correlated by construction. If you split orders across 6 equal venues, venue shares have correlation −1/5-1/5 with no economic link between the venues at all, so do not read a negative correlation of that size as competition.

Question 9.14 ★★★ QR, QT, Trader • market maker, hedge fund, crypto • junior, experienced

You roll a fair die until a six appears. Someone tells you that every roll along the way was even, that is 2, 4 or 6. Given this, what is the expected number of rolls?

Answer

Answer to Question 9.14.

The expected number of rolls is 3/23/2. It is not 3.

Consider stopping at the first roll in {1,3,5,6}\{1, 3, 5, 6\} instead. That stopping time TT is geometric with success probability 4/64/6, so E[T]=3/2E[T] = 3/2. Your event “every roll was even, ending in a six” is exactly the event that this first stopping roll is a six. The value of the stopping roll is independent of when it occurs, since each of the four faces is equally likely at any time. So conditioning on it being a six does not change the distribution of TT.

A direct calculation agrees. The chance of stopping at roll kk with every roll even is (1/3)k−1(1/6)(1/3)^{k-1}(1/6). Summing over kk gives 1/41/4, the chance of the condition, and summing kk times that term gives 3/83/8, so the conditional mean is (3/8)/(1/4)=3/2(3/8)/(1/4) = 3/2.

Classic wrong answer. “Given only evens, the die has three faces, so the wait for a six is geometric with p=1/3p = 1/3, mean 3.” That is rolling a three-sided die, which is a different experiment. In ours, a long sequence must also avoid every odd number, so the condition makes long sequences rare.

Follow-up. Given only that no one was rolled? Stop on {1,6}\{1, 6\}, with success probability 1/31/3, so the mean is 3.

Why it matters. Conditioning on how a path ended changes how long it seems to have lasted. Take a fair random walk with a stop 10 below and a target 5 above: all trades last 50 steps on average, but the winners last 41.741.7 and the losers 66.766.7. A backtest report of “time to target” from winners alone understates the holding time.

Question 9.15 ★★★ QR, QT, Trader • market maker, hedge fund • junior, experienced

A pot holds 100 cooked noodles, which makes 200 loose ends. You pick two loose ends at random and tie them together, and you repeat until no loose ends remain. Some of the tied noodles have closed into loops. What is the expected number of loops?

Answer

Answer to Question 9.15.

The expected number of loops is ∑k=110012k−1≈3.28\sum_{k=1}^{100} \frac{1}{2k-1} \approx 3.28.

Use conditioning on the first step. Take nn noodles, so 2n2n loose ends. Pick one end; its partner is equally likely to be any of the other 2n−12n - 1 ends. Exactly one of those is the other end of the same noodle, and then a loop closes. In every other case two noodles merge into one longer noodle, which behaves exactly like a single noodle. Either way, n−1n - 1 noodles remain, so

Ln=Ln−1+Bernoulli(12n−1),E[Ln]=∑k=1n12k−1.L_n = L_{n-1} + \text{Bernoulli}\Bigl(\frac{1}{2n-1}\Bigr), \qquad E[L_n] = \sum_{k=1}^{n} \frac{1}{2k-1}.

For large nn this is about 12ln⁡n+0.98\tfrac12 \ln n + 0.98, and for n=100n = 100 that gives 3.283.28. Picking two random ends is the same as picking one end and then a random partner, so nothing is lost by the order we used.

Classic wrong answer. Guessing a number proportional to nn. Most ties merge noodles rather than close them, and the loop count grows with the logarithm of nn.

Follow-up. What is the chance that everything ends as one giant loop? No tie closes a loop until the very last: ∏k=21002k−22k−1≈0.089\prod_{k=2}^{100} \frac{2k-2}{2k-1} \approx 0.089.

Question 9.16 ★★★ QR, QT, Trader, QD • market maker, crypto, hedge fund • junior, experienced

100 ants stand at independent uniformly random positions on a stick one metre long. Each faces left or right with equal probability, and all walk at one metre per minute. When two ants meet, both turn around instantly. An ant that reaches either end falls off. On average, how long is it until the last ant falls? What is the expected number of collisions?

Answer

Answer to Question 9.16.

The last ant falls after 100/101≈0.990100/101 \approx 0.990 minutes on average, and there are 1237.51237.5 collisions on average.

Ants are identical, so a collision where both turn around looks exactly like two ants passing through each other, as in Figure 9.2. Follow these “ghosts”. Each ghost walks straight to the end it faces and leaves. Its walking time is the distance to that end. Given a uniform position and a fair direction, that distance is uniform on [0,1][0, 1], independently for each ant. The last fall is the maximum of 100 independent uniforms, whose mean is n/(n+1)=100/101n/(n+1) = 100/101.

Each real collision is one crossing of two ghosts. Two ghosts cross exactly when the left one faces right and the right one faces left, with probability 1/41/4, and they then always meet before either falls. So the expected number is (1002)/4=1237.5\binom{100}{2}/4 = 1237.5.

Two ants on the stick, with time running upwards. Each ant turns at the collision and keeps to its own side. Forget the colours and the same picture shows two straight lines passing through each other, the ghosts. Positions and fall times are the same in both readings.
Figure 9.2. Two ants on the stick, with time running upwards. Each ant turns at the collision and keeps to its own side. Forget the colours and the same picture shows two straight lines passing through each other, the ghosts. Positions and fall times are the same in both readings.

Follow-up. Does a ghost’s fall time belong to the ant it started as? No, and the question did not need it. The fall times are correct as a set, not ant by ant: real ants keep their left-to-right order, so those leaving on the left go in the order they started, and those leaving on the right in reverse order.

Question 9.17 ★★★ QR, QT, Trader • bank, market maker, hedge fund • junior, experienced

You turn over a shuffled 52-card deck one card at a time, and stop once you have seen at least one ace and at least one king. On average, how many cards have you turned over?

Answer

Answer to Question 9.17.

On average you turn over 689/45≈15.31689/45 \approx 15.31 cards.

Let AA be the position of the first ace and KK that of the first king. You stop at max⁡(A,K)\max(A, K). Use the max-min identity:

max⁡(A,K)=A+K−min⁡(A,K).\max(A, K) = A + K - \min(A, K).

The first of mm special cards in a deck of 52 sits on average at 53/(m+1)53/(m+1), by the gap symmetry: mm special cards split the others into m+1m+1 gaps of equal expected size, and the first special card follows one gap. With 4 aces this is E[A]=E[K]=53/5E[A] = E[K] = 53/5. With the 8 aces and kings together it is E[min⁡]=53/9E[\min] = 53/9. So

E[max⁡(A,K)]=2⋅535−539=68945.E[\max(A, K)] = \frac{2 \cdot 53}{5} - \frac{53}{9} = \frac{689}{45}.

Classic wrong answer. Adding the two waits, 53/5+53/553/5 + 53/5. They run at the same time, and they overlap by exactly the time to the first special card.

Follow-up. A biased three-sided die shows its faces with probabilities 12,13,16\tfrac12, \tfrac13, \tfrac16. How many rolls until you have seen all three? Inclusion-exclusion over subsets, with a wait of 1/p1/p for any set of total probability pp: 2+3+6−65−32−2+1=7.32 + 3 + 6 - \frac65 - \frac32 - 2 + 1 = 7.3.

Why it matters. This is how to work out the time for both legs of a paired order to fill. If each leg takes 10 seconds on average, independently and memorylessly, then both are done after 10+10−5=1510 + 10 - 5 = 15 seconds, not 10 and not 20.

Question 9.18 ★★★ QR, QT, Trader, DS • any • junior, experienced

A drawer holds 5 pairs of socks, every pair different, so 10 socks in all. You pull socks out at random, one at a time, until you hold a matching pair. What is the expected number of socks you pull?

Answer

Answer to Question 9.18.

The expected number of socks is 256/63≈4.06256/63 \approx 4.06.

Use the tail-sum formula. Having no pair after kk socks means they come from kk different pairs. There are (5k)\binom{5}{k} ways to choose the pairs and 2k2^k ways to choose one sock from each, out of (10k)\binom{10}{k} draws:

P(T>k)=2k(5k)(10k),k=0,1,…,5.P(T > k) = \frac{2^k \binom{5}{k}}{\binom{10}{k}}, \qquad k = 0, 1, \dots, 5.

These are 1,1,89,23,821,8631, 1, \tfrac89, \tfrac23, \tfrac{8}{21}, \tfrac{8}{63}, and their sum is 256/63256/63. The answer lies between 2 and 6, as it must.

For nn pairs the sum has the closed form 4n/(2nn)4^n / \binom{2n}{n}, which grows like πn\sqrt{\pi n}. For n=5n = 5 the approximation gives 3.963.96, already close.

Classic wrong answer. Using the birthday formula with 55 equally likely types of sock gives about 3.513.51, which is too low. After the first sock, only 1 of the 9 remaining socks matches it, a chance of 1/91/9 and not 1/51/5.

Follow-up. And with 10 pairs? 410/(2010)≈5.684^{10}/\binom{20}{10} \approx 5.68. Doubling the socks multiplies the wait by about 2\sqrt{2}.

Question 9.19 ★★★ QR, QT, Trader • any • junior, experienced

An urn holds 6 red and 4 blue balls. You draw balls at random without replacement and stop as soon as one colour has run out. On average, how many balls are left in the urn when you stop?

Answer

Answer to Question 9.19.

On average 62/35≈1.7762/35 \approx 1.77 balls are left.

Use an indicator for each ball, and ask when it is still in the urn at the end. A red ball is left over exactly when every blue ball is drawn before it. Look only at the relative order of this red ball and the 4 blue ones: all 5 orders of “who comes last” are equally likely, so this happens with probability 1/51/5. Likewise, a blue ball is left over when all 6 red balls come before it, with probability 1/71/7. Summing over balls,

E[left]=65+47=6235.E[\text{left}] = \frac{6}{5} + \frac{4}{7} = \frac{62}{35}.

In general, with rr red and bb blue, it is rb+1+br+1\frac{r}{b+1} + \frac{b}{r+1}.

Classic wrong answer. Modelling the draw with fixed probabilities and a geometric wait. The composition of the urn changes with each draw, and the indicators avoid having to track it.

Follow-up. What is the chance that the balls left are red? They are red exactly when the very last ball in a full draw-down is red, and by symmetry that has probability 6/10=3/56/10 = 3/5.

Follow-up. How many balls do you draw on average? 10−62/35=288/35≈8.2310 - 62/35 = 288/35 \approx 8.23.

Question 9.20 ★★★ QR, DS, Risk, QD, Dev • any • junior, experienced

You throw 100 balls independently into 100 bins, each ball choosing its bin uniformly at random. Find the mean and variance of the number of empty bins.

Answer

Answer to Question 9.20.

The mean is about 36.6036.60 and the variance about 9.749.74, so the standard deviation is about 3.13.1.

Use indicators. Bin ii is empty with probability q1=(0.99)100q_1 = (0.99)^{100}, so the mean is 100 q1≈36.60100\,q_1 \approx 36.60, about 100/e100/e. Bins ii and jj are both empty with probability q2=(0.98)100q_2 = (0.98)^{100}, since every ball must avoid both. Expanding the square of the sum,

Var(X)=100 q1+100⋅99 q2−(100 q1)2≈9.74.\mathrm{Var}(X) = 100\,q_1 + 100 \cdot 99\, q_2 - (100\,q_1)^2 \approx 9.74.

For large nn this tends to n(e−1−2e−2)n(e^{-1} - 2e^{-2}), here 9.729.72.

Classic wrong answer. Treating the count as binomial, 100 q1(1−q1)≈23.2100\,q_1(1 - q_1) \approx 23.2. That more than doubles the true variance. The bins are negatively correlated, with Cov=q2−q12≈−0.00136\mathrm{Cov} = q_2 - q_1^2 \approx -0.00136. If bin 1 is empty, its balls went elsewhere, so other bins are more likely to be filled. This is small per pair, but there are 9 900 ordered pairs.

Why it matters. This gives a quick check on a hash function. With 100 keys and 100 buckets, 50 empty buckets is more than 4 standard deviations above the mean. The binomial variance would have called it under 3, and you might have accepted the hash.

Question 9.21 ★★★ QR, QD, Dev, LLE • any • junior, experienced

Quicksort is given 10 distinct numbers. It picks a pivot uniformly at random from the current list, compares every other number in that list with the pivot, splits the list into the smaller and the larger numbers, and sorts each part the same way. What is the expected number of comparisons? What is the formula for nn numbers?

Answer

Answer to Question 9.21.

For 10 numbers, the expected count is about 24.4424.44 comparisons. In general it is 2(n+1)Hn−4n2(n+1)H_n - 4n, where Hn=∑k≤n1/kH_n = \sum_{k \le n} 1/k.

Use one indicator per pair. Name the numbers by their sorted rank, 1,…,n1, \dots, n, and fix i<ji < j. Two numbers are compared at most once, because a comparison makes one of them the pivot, and a pivot leaves the list. They stay together until the first pivot chosen from the ranks i,i+1,…,ji, i+1, \dots, j. If that pivot is ii or jj, they are compared. If it lies between them, they are separated and never compared. Every number in that block is equally likely to be the first, so

P(i and j compared)=2j−i+1.P(i \text{ and } j \text{ compared}) = \frac{2}{j - i + 1}.

Summing over all pairs gives the formula. For n=10n = 10, 2⋅11⋅H10−40≈24.442 \cdot 11 \cdot H_{10} - 40 \approx 24.44.

Follow-up. Compare with the worst case and with large nn. The worst case is n(n−1)/2=45n(n-1)/2 = 45 for n=10n = 10, when each pivot is the smallest remaining. For large nn, the mean is about 2nln⁡n2n\ln n, or 1.39 nlog⁡2n1.39\, n \log_2 n. For n=1000n = 1000 the exact mean is about 10 986 comparisons, against 499 500 in the worst case.

Question 9.22 ★★★ QR, QT, Trader • hedge fund, market maker • experienced

Ten cards numbered 1 to 10 are dealt at random onto ten squares numbered 1 to 10, one card per square. Every card that lands on its own number is removed, along with its square. The remaining cards are shuffled and dealt again onto the remaining squares, and this repeats until no card is left. What is the expected number of deals?

Answer

Answer to Question 9.22.

The expected number of deals is exactly 10. For any n≥2n \ge 2 it is exactly nn.

The key fact is that a deal of mm cards has exactly one match on average, whatever mm is, by indicators. So one card leaves per deal on average, and nn deals is the natural guess. That is not yet a proof, since the number of deals is random, so prove it by induction. Let pkp_k be the chance of kk matches in a deal of nn cards, with ∑kk pk=1\sum_k k\,p_k = 1. Assume E[Rm]=mE[R_m] = m for all smaller mm; the case m=1m = 1 never arises, since exactly n−1n - 1 matches is impossible. By first-step analysis,

E[Rn]=1+p0E[Rn]+∑k≥1pk(n−k)=1+p0E[Rn]+n(1−p0)−1.E[R_n] = 1 + p_0 E[R_n] + \sum_{k \ge 1} p_k (n - k) = 1 + p_0 E[R_n] + n(1 - p_0) - 1.

So (1−p0)E[Rn]=n(1−p0)(1 - p_0)E[R_n] = n(1 - p_0), and E[Rn]=nE[R_n] = n.

Classic wrong answer. Thinking this needs the derangement probabilities. It does not: they cancel.

Follow-up. How many individual cards are dealt in total? Each deal of mm cards costs mm. Using the same first step with E[k]=1E[k] = 1 and E[k2]=2E[k^2] = 2, the total Sn=n2/2+nS_n = n^2/2 + n satisfies the recursion. For n=10n = 10 that is 60 cards.

Question 9.23 ★★★★ QR, QT • market maker, hedge fund • experienced

You draw independent random numbers, each uniform between 0 and 1, and keep going for as long as each number is larger than the one before it. You stop at the first number that is smaller than the one before it. What is the expected value of that final number?

Answer

Answer to Question 9.23.

The expected value of the final number is e/2−1≈0.359e/2 - 1 \approx 0.359.

Compute by summing over how long the increasing sequence lasts. Fix n≥1n \ge 1 and a value yy. The sequence stops at draw n+1n+1, with that draw equal to yy, when the first nn are increasing and the nn-th exceeds yy. The first nn are increasing with probability 1/n!1/n!. They are increasing and all below yy with probability yn/n!y^n/n!. So the probability we want is (1−yn)/n!(1 - y^n)/n!, and

E[final]=∑n≥1∫01y 1−ynn! dy=∑n≥11n!(12−1n+2).E[\text{final}] = \sum_{n \ge 1} \int_0^1 y\,\frac{1 - y^n}{n!}\,dy = \sum_{n \ge 1} \frac{1}{n!}\Bigl(\frac12 - \frac{1}{n+2}\Bigr).

The first part is (e−1)/2(e-1)/2. For the second, ∑n≥01n!(n+2)=∫01xex dx=1\sum_{n \ge 0} \frac{1}{n!(n+2)} = \int_0^1 x e^x\,dx = 1. Dropping the n=0n = 0 term, which is 12\tfrac12, leaves 12\tfrac12. Hence

E[final]=e−12−12=e2−1.E[\text{final}] = \frac{e - 1}{2} - \frac12 = \frac{e}{2} - 1.

Follow-up. And the number just before the final one, the highest in the sequence? The same method gives ∑n≥11(n−1)!(n+2)=∫01x2ex dx=e−2≈0.718\sum_{n \ge 1} \frac{1}{(n-1)!(n+2)} = \int_0^1 x^2 e^x\,dx = e - 2 \approx 0.718.

Follow-up. How can you check the answer? The number of draws has mean ee, as for any increasing run, and it is a stopping time. So Wald’s identity says the total averages e/2e/2. A simulation confirms the total, the 0.3590.359 and the 0.7180.718.

Question 9.24 ★★★★ QR, QT • market maker, hedge fund, crypto • experienced

You roll a fair die until every face has appeared at least once. When you stop, some faces have appeared exactly once; the last new face always has. What is the expected number of faces that have appeared exactly once?

Answer

Answer to Question 9.24.

The expected number is 1+12+13+14+15+16=49/20=2.451 + \tfrac12 + \tfrac13 + \tfrac14 + \tfrac15 + \tfrac16 = 49/20 = 2.45. In general, with nn faces, it is the harmonic number HnH_n.

Label the faces by the order in which they first appear, and take the kk-th new face, ff. At the moment ff first appears, 6−k6 - k faces are still unseen. Then ff ends up appearing exactly once if it does not come up again before all the unseen faces have appeared.

Ignore every roll that shows neither ff nor one of those unseen faces. The remaining rolls are uniform on 7−k7 - k faces and independent of the past. By symmetry each of these 7−k7 - k faces is equally likely to be the last of them to appear in the future rolls, so ff is last with probability 1/(7−k)1/(7-k). If ff is last, every unseen face comes before its next appearance. So the kk-th new face is a singleton with probability 1/(7−k)1/(7-k), and by linearity

E[singletons]=∑k=1617−k=H6=2.45.E[\text{singletons}] = \sum_{k=1}^{6} \frac{1}{7-k} = H_6 = 2.45.

Classic wrong answer. Using a Poisson count at the expected stopping time: with 14.714.7 rolls, each face appears once with chance about (14.7/6) e−14.7/6(14.7/6)\,e^{-14.7/6}, so the total is about 1.271.27. The stopping time depends on the counts: the rule ends the game as soon as the last new face appears, so that face is always a singleton. Treating the time as fixed misses this.

Follow-up. With a 52-card deck, sampling with replacement until you have seen all cards? H52≈4.54H_{52} \approx 4.54. It grows very slowly, while the total wait grows like nln⁡nn \ln n.

Question 9.25 ★★★★ QR, QT • hedge fund, market maker • experienced

You roll a fair die until every face has appeared at least twice. Set up an exact calculation that an interviewer could follow on a whiteboard, and give the expected number of rolls to two decimal places.

Answer

Answer to Question 9.25.

The expected number of rolls is about 24.1324.13.

Poissonisation makes the faces independent. Imagine rolls arriving as a Poisson process of rate one per unit time. Each face then appears as an independent Poisson process of rate 1/61/6. The expected stopping time in clock time equals the expected number of rolls, because each roll takes one unit on average and the stopping rule looks only at the rolls.

By time tt, with u=t/6u = t/6, a given face has appeared at least twice with probability 1−e−u(1+u)1 - e^{-u}(1 + u). All six have done so with the sixth power of this. Integrating the tail,

E[rolls]=∫0∞P(τ>t) dt=6∫0∞[1−(1−e−u(1+u))6]du.E[\text{rolls}] = \int_0^\infty P(\tau > t)\,dt = 6 \int_0^\infty \Bigl[1 - \bigl(1 - e^{-u}(1 + u)\bigr)^6\Bigr] du.

Expand by the binomial theorem. Each term is an elementary integral:

E[rolls]=6∑k=16(−1)k+1(6k)∫0∞e−ku(1+u)k du,E[\text{rolls}] = 6 \sum_{k=1}^{6} (-1)^{k+1} \binom{6}{k} \int_0^\infty e^{-ku}(1 + u)^k\,du,

where the k=1k = 1 integral is 22, the k=2k = 2 integral is 5/45/4, and so on. The sum comes to 24.1324.13.

An alternative that you can run as a table is a Markov chain on (a,b)(a, b), the numbers of faces seen zero times and once, with E(a,b)=[6+aE(a−1,b+1)+bE(a,b−1)]/(a+b)E(a,b) = \bigl[6 + a E(a-1, b+1) + b E(a, b-1)\bigr]/(a+b) and E(0,0)=0E(0,0)=0.

Classic wrong answer. Doubling the single-collection wait to 29.429.4. Collecting the first full set leaves duplicates, which count towards the second. Only 9.439.43 more rolls are needed on average beyond the 14.714.7.