Skip to content
LeetQuant
QT02 Advanced Probability and Brainteasers

QT02 · Advanced Probability and Brainteasers · Lesson 1 of 5

Classic Interview Puzzles

A guided tour of the puzzles that come up again and again, and the patterns behind them.

30 min

Puzzles are patterns in disguise

Interviewers reuse a small number of ideas with new numbers and new stories. Recognising the idea is most of the work. This lesson covers four families: waiting for patterns, races between events, backward induction, and optimising a schedule.

Waiting for a pattern

Expected waiting times for coin patterns are solved by first-step analysis on "how much of the pattern is matched". For two heads in a row, with E0E_0 the expected flips from scratch and E1E_1 after a single head:

E0=1+12E1+12E0,E1=1+12⋅0+12E0,E_0 = 1 + \tfrac12 E_1 + \tfrac12 E_0, \qquad E_1 = 1 + \tfrac12 \cdot 0 + \tfrac12 E_0 ,

which gives E0=6E_0 = 6.

There is also a shortcut for a fair coin. For each kk such that the first kk symbols of the pattern equal its last kk symbols (including kk = the full length), add 2k2^k:

E[wait for HH]=21+22=6,E[wait for HT]=22=4.\mathbb{E}[\text{wait for HH}] = 2^1 + 2^2 = 6, \qquad \mathbb{E}[\text{wait for HT}] = 2^2 = 4 .

Self-overlap makes a pattern slower to appear. After a near miss, an overlapping pattern often has to start again from little progress, while a non-overlapping one keeps the progress it had. For a die, replace 22 with 66: two sixes in a row takes 6+36=426 + 36 = 42 rolls on average.

Worked example

waiting for HTHT

A fair coin is flipped until HTHT appears. What is the expected number of flips?

Show solution

Compare prefixes with suffixes:

  • k=1k = 1: H vs T, no.
  • k=2k = 2: HT vs HT, yes, contributing 22=42^2 = 4.
  • k=3k = 3: HTH vs THT, no.
  • k=4k = 4: the whole pattern, contributing 24=162^4 = 16.

So the expected wait is 4+16=204 + 16 = 20, compared with 1616 for a non-overlapping four-flip pattern such as HHTT.

Races between events

"Which happens first?" questions are solved by ignoring everything irrelevant. If each trial produces AA with probability aa, BB with probability bb, and otherwise nothing, then

P(A before B)=aa+b,P(A \text{ before } B) = \frac{a}{a + b} ,

because only the first trial that is AA or BB matters. For alternating turns, condition on the first round: if player 1 wins on a turn with probability pp, then P(player 1 wins)=p+(1−p)2 P(player 1 wins)P(\text{player 1 wins}) = p + (1-p)^2\,P(\text{player 1 wins}), so it equals p1−(1−p)2=12−p\dfrac{p}{1 - (1-p)^2} = \dfrac{1}{2 - p}.

For races between patterns, set up states as before, but with two absorbing states instead of one.

Backward induction

For games between rational players, solve the smallest case first and work up. Each player compares their offer with what they would get if the game moved to the next, smaller case.

In the pirate game with the "at least half the votes" rule:

  • Two pirates (D, E). D votes for their own plan, which is half the votes, so D keeps everything: (100,0)(100, 0).
  • Three pirates (C, D, E). C needs one more vote. E gets 0 if C is thrown overboard, so E accepts 1 coin: (99,0,1)(99, 0, 1).

Each extra pirate buys the votes of those who would get nothing in the next case, paying one coin more than that next case would give them. Keep going and you can solve any number of pirates.

Optimising a schedule

For crossing puzzles (the bridge, boats, lamps), write down the two or three candidate strategies and compare totals, rather than searching blindly. For the bridge with crossing times t1≤t2≤t3≤t4t_1 \le t_2 \le t_3 \le t_4, the two ways to get the slowest pair over are:

  • Fastest pair shuttles: t1,t2t_1, t_2 cross; t1t_1 returns; t3,t4t_3, t_4 cross; t2t_2 returns; t1,t2t_1, t_2 cross. Total t1+3t2+t4t_1 + 3t_2 + t_4.
  • Fastest escorts each: t1t_1 escorts t4t_4, returns, escorts t3t_3, returns, crosses with t2t_2. Total 2t1+t2+t3+t42t_1 + t_2 + t_3 + t_4.

The first is better exactly when 2t2<t1+t32t_2 < t_1 + t_3: the two slow people should cross together when the second-fastest is fast enough to make the extra return trip cheap.

Worked example

four people, times 1, 3, 6 and 8

Using the rules above, what is the minimum time to get people with crossing times 1, 3, 6 and 8 minutes across?

Show solution

Shuttle: 1+3⋅3+8=181 + 3 \cdot 3 + 8 = 18. Escort: 2⋅1+3+6+8=192 \cdot 1 + 3 + 6 + 8 = 19. Since 2t2=6<t1+t3=72t_2 = 6 < t_1 + t_3 = 7, shuttling wins, and the answer is 18 minutes.

Knowledge check

What is the expected number of fair coin flips until three heads in a row (HHH)?

Knowledge check

You roll two dice repeatedly. What is the probability that a sum of 7 appears before a sum of 6?

Knowledge check

Two players alternate shooting free throws, each scoring with probability 1/3. The first to score wins. What is the probability that the first shooter wins?

Practice problems

Finished reading?

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