KatInterview

Calculate Expected Score from Multi-Stage Die Rerolls

Math & Bit ManipulationAuto-graded · 14 test cases
EasyAuto-gradedMath & Bit Manipulationmathprobabilityexpected-value

Last updated

Computes the optimal decision threshold and overall expected payoff when given one optional reroll on an N-sided die.

You are playing a game with a fair $N$-sided die whose faces are labeled from $1$ to $N$. You roll the die once and observe the outcome. After seeing this result, you may either keep the current outcome as your score or choose to reroll the die once. If you choose to reroll, the second outcome completely replaces the first roll and becomes your final score, regardless of whether it is higher or lower.

Assuming optimal play to maximize the final score, determine:

  1. The list of initial outcomes (in strictly increasing order) for which you should choose to reroll (i.e., outcomes where the expected value of rerolling is strictly greater than keeping the initial roll).
  2. The total expected payoff (final score) of the game under this strategy.

Return a dictionary with two keys:

  • "reroll_values": A list of integers representing the initial roll outcomes where rerolling is strictly optimal.
  • "expected_value": A float representing the expected final score.

Constraints

  • $1 \le N \le 1000$

Examples

Example 1

Input: optimal_die_reroll_strategy(6)

Output: {'reroll_values': [1, 2, 3], 'expected_value': 4.25}

Hints

Hint 1

The expected outcome of a single reroll is independent of the first roll and equals the mean of the die's faces: (N + 1) / 2.

Hint 2

Compare the value of holding the current outcome x against the expected value of rerolling.

Hint 3

Reroll whenever the current outcome x is strictly less than the expected value of a single roll.