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 .
Use the tail-sum formula: for a variable taking values , the mean equals , because each outcome is counted once for every up to . The higher die is at least unless both dice are below , so . Summing over ,
Follow-up. And with three dice? Cubes replace squares: .
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 , and the difference averages .
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 is geometric with mean 6, and each roll has mean . Whether you stop after a roll depends only on the rolls so far, so Wald’s identity applies: . You can check this with first-step analysis. A roll that is not a six has mean 3, so
which gives .
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 .
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 . The total is . 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 . There are 1 225 disjoint pairs, each with probability , where splits on whether the second pair’s first card has the rank just used. Then .
Follow-up. How likely is a shuffle with no such pair? About 4.5%, by simulation. A Poisson with mean 3 predicts , 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 matches with probability , so the mean is . For the variance, write as the sum of the indicators plus the sum over ordered pairs of positions of . Positions and both match with probability , and there are ordered pairs, so and .
Classic wrong answer. Treating the positions as independent gives a variance of . The small positive covariance between positions supplies the rest.
Follow-up. What is the chance of no match at all? Very close to , 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 rolls.
This is the coupon collector. Split the wait into stages. When you have seen faces, each roll shows a new one with probability , so that stage lasts rolls on average. Adding the stages,
Follow-up. What is the variance? The stages are independent geometrics, each with variance . Their sum is about , a standard deviation of about 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 .
Why it matters. The cost is , not . 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 , so by indicators the mean is . The changes are independent, not only pairwise: given any toss, the next one differs with probability whatever came before. So the number of changes is Binomial, with variance .
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 , and there are 96 such starting places. A run at either end needs only HHHT or THHH, with probability each. The total is .
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 and standard deviation about , 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 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 cards on average. The second ace comes after two gaps and two aces:
Classic wrong answer. forgets that the aces occupy positions too.
Follow-up. Where is the last ace on average? The same count gives the -th ace at position , so the fourth is at .
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 .
Use the tail-sum formula. The lowest of the three is at least when all three dice are at least , which has probability . Summing from to and writing ,
using the fact that the sum of the first cubes is the square of the sum of the first 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 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 to , it is .
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 . For the hash table, about 41 keys.
Use the tail-sum formula, . The wait exceeds when the first rolls are all different, which has probability . The terms for are
and their sum is . Note that the wait is at least 2 and at most 7.
For buckets the same sum is the birthday problem. Its large- value is about , which for gives ; the exact sum is also . 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 numbers.
Let be the number of draws. Then exactly when the first numbers sum to at most 1, and that has probability . To see it, change variables to the running totals . The map has determinant one, so the probability is the volume of the region , and that region is one of the equal orderings of points in the unit cube. The tail-sum formula gives
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, .
Follow-up. What is the variance of ? , so the variance is .
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 be the number of further draws. means the first number is the largest of the first , and by symmetry that has probability . So , and . The tail-sum formula gives the harmonic series,
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 . 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 , and the variance is about .
Use indicators. The -th number is a record when it is the largest of the first , with probability , so . The large- form is , here about .
For the variance you need a stronger fact: the indicators are independent. Knowing how the first numbers are ordered among themselves says nothing about where the -th ranks among the first , and the record events depend only on those relative ranks. So the variance is the sum of the individual Bernoulli variances:
Why it matters. Records in pure noise are common early and rare later: a year of 252 independent daily results has about 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 be the number of ones and the number of twos. What are the covariance and the correlation of and ?
Answer
Answer to Question 9.13.
The covariance is and the correlation is .
Write and , where marks roll showing a one, and roll showing a two. Different rolls are independent, so only the same roll contributes to the covariance. A single roll cannot be both, so , and
Over 60 rolls this gives . Each count has variance , so the correlation is , whatever the number of rolls.
Follow-up. Check it another way. counts rolls showing a one or a two, which is Binomial with variance . And indeed .
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 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 . It is not 3.
Consider stopping at the first roll in instead. That stopping time is geometric with success probability , so . 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 .
A direct calculation agrees. The chance of stopping at roll with every roll even is . Summing over gives , the chance of the condition, and summing times that term gives , so the conditional mean is .
Classic wrong answer. “Given only evens, the die has three faces, so the wait for a six is geometric with , 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 , with success probability , 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 and the losers . 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 .
Use conditioning on the first step. Take noodles, so loose ends. Pick one end; its partner is equally likely to be any of the other 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, noodles remain, so
For large this is about , and for that gives . 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 . Most ties merge noodles rather than close them, and the loop count grows with the logarithm of .
Follow-up. What is the chance that everything ends as one giant loop? No tie closes a loop until the very last: .
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 minutes on average, and there are 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 , independently for each ant. The last fall is the maximum of 100 independent uniforms, whose mean is .
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 , and they then always meet before either falls. So the expected number is .
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 cards.
Let be the position of the first ace and that of the first king. You stop at . Use the max-min identity:
The first of special cards in a deck of 52 sits on average at , by the gap symmetry: special cards split the others into gaps of equal expected size, and the first special card follows one gap. With 4 aces this is . With the 8 aces and kings together it is . So
Classic wrong answer. Adding the two waits, . 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 . How many rolls until you have seen all three? Inclusion-exclusion over subsets, with a wait of for any set of total probability : .
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 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 .
Use the tail-sum formula. Having no pair after socks means they come from different pairs. There are ways to choose the pairs and ways to choose one sock from each, out of draws:
These are , and their sum is . The answer lies between 2 and 6, as it must.
For pairs the sum has the closed form , which grows like . For the approximation gives , already close.
Classic wrong answer. Using the birthday formula with equally likely types of sock gives about , which is too low. After the first sock, only 1 of the 9 remaining socks matches it, a chance of and not .
Follow-up. And with 10 pairs? . Doubling the socks multiplies the wait by about .
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 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 . Likewise, a blue ball is left over when all 6 red balls come before it, with probability . Summing over balls,
In general, with red and blue, it is .
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 .
Follow-up. How many balls do you draw on average? .
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 and the variance about , so the standard deviation is about .
Use indicators. Bin is empty with probability , so the mean is , about . Bins and are both empty with probability , since every ball must avoid both. Expanding the square of the sum,
For large this tends to , here .
Classic wrong answer. Treating the count as binomial, . That more than doubles the true variance. The bins are negatively correlated, with . 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 numbers?
Answer
Answer to Question 9.21.
For 10 numbers, the expected count is about comparisons. In general it is , where .
Use one indicator per pair. Name the numbers by their sorted rank, , and fix . 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 . If that pivot is or , 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
Summing over all pairs gives the formula. For , .
Follow-up. Compare with the worst case and with large . The worst case is for , when each pivot is the smallest remaining. For large , the mean is about , or . For 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 it is exactly .
The key fact is that a deal of cards has exactly one match on average, whatever is, by indicators. So one card leaves per deal on average, and deals is the natural guess. That is not yet a proof, since the number of deals is random, so prove it by induction. Let be the chance of matches in a deal of cards, with . Assume for all smaller ; the case never arises, since exactly matches is impossible. By first-step analysis,
So , and .
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 cards costs . Using the same first step with and , the total satisfies the recursion. For 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 .
Compute by summing over how long the increasing sequence lasts. Fix and a value . The sequence stops at draw , with that draw equal to , when the first are increasing and the -th exceeds . The first are increasing with probability . They are increasing and all below with probability . So the probability we want is , and
The first part is . For the second, . Dropping the term, which is , leaves . Hence
Follow-up. And the number just before the final one, the highest in the sequence? The same method gives .
Follow-up. How can you check the answer? The number of draws has mean , as for any increasing run, and it is a stopping time. So Wald’s identity says the total averages . A simulation confirms the total, the and the .
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 . In general, with faces, it is the harmonic number .
Label the faces by the order in which they first appear, and take the -th new face, . At the moment first appears, faces are still unseen. Then 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 nor one of those unseen faces. The remaining rolls are uniform on faces and independent of the past. By symmetry each of these faces is equally likely to be the last of them to appear in the future rolls, so is last with probability . If is last, every unseen face comes before its next appearance. So the -th new face is a singleton with probability , and by linearity
Classic wrong answer. Using a Poisson count at the expected stopping time: with rolls, each face appears once with chance about , so the total is about . 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? . It grows very slowly, while the total wait grows like .
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 .
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 . 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 , with , a given face has appeared at least twice with probability . All six have done so with the sixth power of this. Integrating the tail,
Expand by the binomial theorem. Each term is an elementary integral:
where the integral is , the integral is , and so on. The sum comes to .
An alternative that you can run as a table is a Markov chain on , the numbers of faces seen zero times and once, with and .
Classic wrong answer. Doubling the single-collection wait to . Collecting the first full set leaves duplicates, which count towards the second. Only more rolls are needed on average beyond the .