Calculate Expected Score from Multi-Stage Die Rerolls
Math & Bit ManipulationAuto-graded · 14 test casesLast 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:
- 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).
- 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.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
To maximize the expected final score:
-
Calculate the Expected Value of Rerolling: If we decide to reroll, we receive a random outcome uniformly distributed from $1$ to $N$. The expected value of a single die roll is: $$E[\text{reroll}] = \frac{N + 1}{2}$$ This expectation does not depend on the outcome of the first roll.
-
Determine the Optimal Decision Rule: For any first roll outcome $x \in {1, 2, \dots, N}$:
- If we keep $x$, our expected score is $x$.
- If we reroll, our expected score is $\frac{N + 1}{2}$.
To strictly maximize expected value, we should reroll whenever the expected return from rerolling is strictly greater than the initial outcome: $$x < \frac{N + 1}{2}$$ Thus, the list of reroll outcomes consists of all integers $x \in [1, N]$ satisfying $x < \frac{N + 1}{2}$.
-
Compute the Expected Final Score: Each of the $N$ possible outcomes for the first roll occurs with probability $\frac{1}{N}$. Under optimal decision-making, the payoff achieved for a first roll $x$ is $\max\left(x, \frac{N + 1}{2}\right)$. The overall expected value is: $$E[\text{game}] = \frac{1}{N} \sum_{x=1}^{N} \max\left(x, \frac{N + 1}{2}\right)$$
Complexity
- Time Complexity: $\mathcal{O}(N)$ to iterate over all possible die faces from $1$ to $N$.
- Space Complexity: $\mathcal{O}(N)$ to construct the output list of reroll values.
def optimal_die_reroll_strategy(sides: int) -> dict:
reroll_value = (sides + 1) / 2
reroll_values = [value for value in range(1, sides + 1) if value < reroll_value]
expected_value = sum(max(value, reroll_value) for value in range(1, sides + 1)) / sides
return {
"reroll_values": reroll_values,
"expected_value": expected_value,
}