Maximum Expected Value from Sequential Uniform Draws
Dynamic ProgrammingAuto-graded · 23 test casesLast updated
Tests understanding of optimal stopping via backward induction and dynamic threshold computation.
You observe a sequence of n independent random values, each drawn uniformly from the interval [0, 1]. The values are revealed one at a time. After seeing each value, you must immediately decide whether to keep it (ending the process) or discard it and proceed to the next value. If you reach the final value, you must keep it.
Your goal is to maximize the expected value of the number you ultimately keep.
Implement a function that computes the maximum expected value achievable when n values will be drawn, assuming you play optimally.
Constraints
1 ≤ n ≤ 1000- All draws are independent and identically distributed as
Uniform(0, 1) - You must decide immediately upon seeing each value (no looking ahead)
- If you discard the first
n-1values, you must accept the last one
Examples
Example 1
Input: max_expected_value(1)
Output: 0.5
Example 2
Input: max_expected_value(2)
Output: 0.625
Example 3
Input: max_expected_value(3)
Output: 0.6953125
Example 4
Input: max_expected_value(5)
Output: 0.7750815008766949
Hints
Hint 1
Work backwards: what is the expected value when only one draw remains?
Hint 2
At each step, compare the current value against the expected payoff from continuing optimally.
Hint 3
The optimal threshold decreases as you get closer to the end of the sequence.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is an optimal stopping problem solved using backward induction. We build the solution from the end of the sequence backward to the beginning.
Base case: With only one draw remaining, you have no choice but to accept it. Since the draw comes from Uniform(0, 1), the expected value is 0.5.
Recursive step: Suppose you know the optimal expected value V when k draws remain. Now consider the case with k+1 draws. When you observe the current value x, you face a choice:
- Stop now: receive
x - Continue: discard
xand proceed optimally withkremaining draws, getting expected valueV
The optimal decision is to stop if and only if x ≥ V. This means:
- With probability
V, the draw falls below the threshold and you continue (gettingV) - With probability
1-V, the draw exceeds the threshold and you stop (getting the average of values in[V, 1], which is(V+1)/2)
Therefore:
E[max(X, V)] = V · V + (1-V) · (V+1)/2
= V² + (1-V²)/2
= (1 + V²)/2
This gives us the recurrence: starting from V₁ = 0.5, we compute Vₖ₊₁ = (1 + Vₖ²)/2 for each additional draw.
Why this is correct: At each step, we compare the observed value against the best achievable continuation value. By Bellman's principle of optimality, this greedy comparison yields the globally optimal strategy. The thresholds naturally decrease as you approach the end (you become less selective when running out of chances).
Time complexity: O(n) — one iteration per draw beyond the first
Space complexity: O(1) — only tracking the current expected value
def max_expected_value(n: int) -> float:
value = 0.5
for _ in range(1, n):
value = (1.0 + value * value) / 2.0
return value