KatInterview

Dice Duel Probability with Optional Blind Reroll

Math & Bit ManipulationAuto-graded · 21 test cases
EasyAuto-gradedMath & Bit Manipulationmathprobabilitygreedy

Last updated

Calculate the exact win probability in a dice duel where one player uses an optimal single blind reroll strategy.

Two players, Alice and Bob, play a dice game. Alice rolls an $m$-sided die with integer face values uniformly distributed from $1$ to $m$. Bob rolls an $n$-sided die with integer face values uniformly distributed from $1$ to $n$. All rolls are mutually independent.

Bob wants to maximize his probability of winning. He sees the result of his first roll, but does not see Alice's outcome. Bob may choose to reroll his die at most once. If he rerolls, he must keep the second roll's result and discard the first roll. Alice has no rerolls.

After Bob establishes his final number and Alice rolls her die:

  • Alice wins if her final roll is strictly greater than Bob's final roll.
  • Bob wins if his final roll is greater than or equal to Alice's final roll (ties are awarded to Bob).

Assuming Bob plays optimally to maximize his chance of winning, return the probability that Alice wins.

Constraints

  • $1 \le m \le 1000$ (represented as alice_sides)
  • $1 \le n \le 1000$ (represented as bob_sides)

Examples

Example 1

Input: alice_win_probability(30, 20)

Output: 0.5666666666666668

Example 2

Input: alice_win_probability(1, 1)

Output: 0

Example 3

Input: alice_win_probability(6, 6)

Output: 0.2916666666666667

Hints

Hint 1

Determine Bob's win probability for a fixed final roll $b$.

Hint 2

Since Bob does not see Alice's roll before deciding to reroll, the expected win probability of a second roll is fixed and independent of his first roll.

Hint 3

Bob should keep his first roll $b$ if and only if his win probability with $b$ is greater than or equal to his expected win probability from rerolling.