Skip to content
LeetQuant
QT04 Random Walks and Optimal Stopping

QT04 · Random Walks and Optimal Stopping · Lesson 2 of 4

Optimal Stopping and the Secretary Problem

Decide when to stop: continuation values, threshold rules, backward induction over a finite horizon, the one-step lookahead rule, and the secretary problem with its 1/e answer.

18 min

The question

You watch a sequence of random rewards and may stop at most once, collecting the reward in front of you. Rewards you pass up are gone. When should you stop? Interviewers ask this as dice games, card games, house offers and hiring puzzles, and the answer always has the same shape:

Stop when what you have now is at least what you expect to get by continuing optimally.

The quantity on the right is the continuation value. Everything in this lesson is a way of computing it.

A stopping rule may use only what you have seen so far. "Stop at the largest value" is not a rule; "stop at the first value above 0.7" is.

Finite horizon: work backwards

Suppose you will see at most nn i.i.d. offers X1,…,XnX_1, \dots, X_n and must accept one. Let VkV_k be the value of the game with kk offers still to come, playing optimally. With one offer left you must take it, so V1=E[X]V_1 = \mathbb{E}[X]. With kk left, you see an offer xx and compare it with Vk−1V_{k-1}:

Vk=E[max⁡(X, Vk−1)].V_k = \mathbb{E}\big[\max(X,\, V_{k-1})\big] .

The optimal rule is a threshold rule: with kk offers left (this one included), accept iff x≥Vk−1x \ge V_{k-1}. The thresholds fall as the deadline approaches, because the option to keep searching is worth less when there is less time to use it.

For X∼Uniform(0,1)X \sim \text{Uniform}(0, 1) and a threshold t∈[0,1]t \in [0, 1], E[max⁡(X,t)]=t⋅t+∫t1x dx=12(1+t2)\mathbb{E}[\max(X, t)] = t \cdot t + \int_t^1 x\,dx = \tfrac12(1 + t^2), so

V1=12,Vk=12(1+Vk−12).V_1 = \tfrac12, \qquad V_{k} = \tfrac12\big(1 + V_{k-1}^2\big) .
Offers left kk12345
VkV_k0.50.50.6250.6250.69530.69530.74170.74170.77510.7751

Vk→1V_k \to 1 slowly: with many offers you can afford to be picky, but each extra offer adds less.

Worked example

three days to sell

You must sell a car within three days. Each day brings one offer, uniform on [0,1][0, 1] (in units of $10,000), independent across days. An offer must be accepted or rejected on the day. What is your strategy and its expected sale price?

Show solution

Work from the last day.

  • Day 3 (one offer left): take whatever comes. Value V1=12V_1 = \tfrac12.
  • Day 2 (two left): accept iff x≥V1=0.5x \ge V_1 = 0.5. Value V2=12(1+0.25)=58V_2 = \tfrac12(1 + 0.25) = \tfrac58.
  • Day 1 (three left): accept iff x≥V2=0.625x \ge V_2 = 0.625. Value V3=12(1+2564)=89128≈0.695V_3 = \tfrac12\big(1 + \tfrac{25}{64}\big) = \tfrac{89}{128} \approx 0.695.

So: accept on day 1 only above $6,250, on day 2 above $5,000, and take anything on day 3. Expected price about $6,953, against $5,000 for accepting the first offer.

Notice what the thresholds are: each one is the value of the remaining game. Say that out loud in an interview; it is the whole argument.

The one-step lookahead rule

Some problems have no fixed horizon. A useful shortcut is the one-step lookahead (OLA) rule: stop as soon as stopping now is at least as good as continuing for exactly one more step and then stopping.

OLA is optimal when the problem is monotone: once the "stop now beats one more step" condition holds, it keeps holding whatever happens next. Then there is never a reason to continue past the first state where it holds, because from there on you would stop at the next step anyway.

Dice example. Roll a die repeatedly, adding the faces to a running total. If you roll a 1, the total is wiped out and the game ends with nothing. You may stop at any time and keep the total. With total ss, one more roll changes your total by

16(2+3+4+5+6)−16s=20−s6.\frac16(2 + 3 + 4 + 5 + 6) - \frac16 s = \frac{20 - s}{6} .

This is positive iff s<20s < 20. The total only goes up while you keep rolling (or the game ends), so once s≥20s \ge 20 the condition holds for ever: the problem is monotone and OLA is optimal. Stop at the first total of 20 or more. An exact calculation over all paths gives an expected payoff of about 8.148.14, and stopping at 20 or at 21 gives the same value, because at exactly 20 you are indifferent.

The secretary problem

nn candidates arrive in random order. After each interview you know only how the candidate ranks against those already seen, and you must hire or reject on the spot. You win only if you hire the best of all nn. How do you maximise the chance of winning?

Why a cutoff rule. Only a candidate who is the best so far (a "relative best") can be the overall best. A relative best at position ii is the overall best with probability i/ni/n, because the best of the first ii is the best of all nn exactly when the overall best is among the first ii. That probability increases with ii, while the value of continuing decreases with ii (fewer candidates left). So there is a cutoff: reject the first r−1r - 1 candidates, then hire the first relative best.

The success probability. Fix the cutoff r≥2r \ge 2. You win iff the best candidate is at some position i≥ri \ge r and you have not already hired someone between rr and i−1i - 1. The second condition holds exactly when the best of the first i−1i - 1 candidates sits among the first r−1r - 1 (the skipped block), which has probability r−1i−1\tfrac{r-1}{i-1}. So

P(r)=∑i=rn1n⋅r−1i−1=r−1n∑i=rn1i−1.P(r) = \sum_{i=r}^{n} \frac1n \cdot \frac{r - 1}{i - 1} = \frac{r - 1}{n} \sum_{i=r}^{n} \frac{1}{i - 1} .

The limit. Write x=r/nx = r/n. The sum is a Riemann sum for ∫x1dtt\int_x^1 \frac{dt}{t}, so

P≈xln⁡1x.P \approx x \ln\frac1x .

Differentiating, ln⁡1x−1=0\ln\frac1x - 1 = 0, so x=1/ex = 1/e and P=1e≈0.368P = \tfrac1e \approx 0.368.

The rule: skip about n/e≈37%n/e \approx 37\% of the candidates, then take the first one better than all of them. You win about 37% of the time, however large nn is.

Exact optimal cutoffs for small nn:

nnSkipP(best)P(\text{best})
311/21/2
4111/24≈0.45811/24 \approx 0.458
5213/30≈0.43313/30 \approx 0.433
1030.39870.3987
100370.37100.3710

The table comes from this script:

from fractions import Fraction


def success(n, r):
    """P(hire the best) when skipping r - 1 candidates."""
    if r == 1:
        return Fraction(1, n)
    return Fraction(r - 1, n) * sum(Fraction(1, i - 1) for i in range(r, n + 1))


for n in (4, 10, 100):
    r = max(range(1, n + 1), key=lambda r: success(n, r))
    print(n, r - 1, round(float(success(n, r)), 4))

Output:

4 1 0.4583
10 3 0.3987
100 37 0.371

Worked example

the secretary problem with four candidates

Four candidates arrive in random order. Compute the success probability of each cutoff and pick the best.

Show solution

Use P(r)=r−14∑i=r41i−1P(r) = \frac{r - 1}{4}\sum_{i=r}^{4}\frac{1}{i - 1}.

  • Skip 0 (r=1r = 1): you take the first candidate, who is best with probability 14\tfrac14.
  • Skip 1 (r=2r = 2): 14(1+12+13)=14⋅116=1124≈0.458\tfrac14\big(1 + \tfrac12 + \tfrac13\big) = \tfrac14 \cdot \tfrac{11}{6} = \tfrac{11}{24} \approx 0.458.
  • Skip 2 (r=3r = 3): 24(12+13)=512≈0.417\tfrac24\big(\tfrac12 + \tfrac13\big) = \tfrac{5}{12} \approx 0.417.
  • Skip 3 (r=4r = 4): 34⋅13=14\tfrac34 \cdot \tfrac13 = \tfrac14 (you always take the last candidate).

The best rule skips one candidate. Check by direct reasoning: with r=2r = 2 you fail only if the first candidate is the best (probability 14\tfrac14) or a relative best other than the overall best appears before the best. Enumerating the 24 orders confirms that 11 of them succeed.

Compare n/e=1.47n/e = 1.47: the asymptotic rule says skip one or two, and the exact calculation picks one.

When stopping cannot help

Cards are turned over one at a time from a shuffled deck of 26 red and 26 black. At any moment you may say "next", and you win if the next card is red. Can you beat 12\tfrac12?

No. Let MkM_k be the fraction of red cards among those not yet turned after kk cards. Given the past, the next card is red with probability MkM_k, and after it is turned the expected new fraction is again MkM_k: MM is a martingale. Your win probability when you say "next" at time τ\tau is E[Mτ]=M0=12\mathbb{E}[M_\tau] = M_0 = \tfrac12 for any stopping rule (the deck is finite, so the optional stopping theorem applies). Waiting until more black cards have gone helps on some paths and hurts on others by exactly the same amount.

The general lesson: a stopping rule cannot create value from a fair game. Stopping helps only when the payoff process is not a martingale, for example when you collect the maximum of something, or when there is a cost, a deadline or a drift. The next lesson develops this.

Knowledge check

In the secretary problem with 10 candidates, which rule maximises the probability of hiring the best one?

Knowledge check

You get two offers, each uniform on [0,1][0, 1], and must accept one of them, deciding on the first before seeing the second. What is the optimal rule and its value?

Knowledge check

You roll a die and add up the faces, but a 1 wipes out the total and ends the game. You may stop and keep the total at any time. When should you stop?

Key takeaways

  • Stop iff the current payoff is at least the continuation value; the whole problem is computing that value.
  • Finite horizon with i.i.d. offers: Vk=E[max⁡(X,Vk−1)]V_k = \mathbb{E}[\max(X, V_{k-1})], and the threshold with kk offers left is Vk−1V_{k-1}. For uniform offers, Vk=12(1+Vk−12)V_k = \tfrac12(1 + V_{k-1}^2).
  • In monotone problems the one-step lookahead rule is optimal; the die-with-a-bust game stops at 20.
  • Secretary problem: skip about n/en/e, then take the first relative best; success probability tends to 1/e1/e. Small cases: n=4n = 4 skip 1 (1124\tfrac{11}{24}), n=10n = 10 skip 3 (0.3990.399).
  • No stopping rule beats a martingale: the red-card game is worth exactly 12\tfrac12.

Practice problems

Finished reading?

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