KatInterview

Maximum Expected Value from Sequential Uniform Draws

Dynamic ProgrammingAuto-graded · 23 test cases
MediumAuto-gradedDynamic Programmingoptimal-stoppingbackward-inductionexpected-value

Last 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-1 values, 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.