KatInterview

Reroll Game Expected Value and Break-Even Cost

Math & Bit ManipulationAuto-graded · 21 test cases
MediumAuto-gradedMath & Bit Manipulationprobabilityexpected-valuerecursion

Last updated

Tests probability recursion, expected value with random stopping times, and cost-benefit analysis in a sequential game.

You are analyzing a sequential dice game with the following rules:

  1. A biased die is rolled repeatedly
  2. After each roll, you collect points equal to the face value
  3. With probability p, the roll forces you to continue (roll again)
  4. With probability 1 - p, the roll ends the game
  5. The expected value of a single roll is mu

Your task is to compute two quantities:

  1. Expected total payout: What is the expected sum of all face values collected before the game ends?
  2. Break-even cost: If each roll after the first costs c points, what value of c makes the game fair (expected net payout = 0)?

Return a list [expected_payout, break_even_cost].

Special cases:

  • If p >= 1, the game never terminates; return [None, None]
  • If p == 0, only one roll occurs; return [mu, None] (no rerolls mean no reroll cost applies)

Constraints

  • 0 <= p <= 1
  • mu > 0 (expected roll value is positive)
  • Return floating-point values with standard precision
  • The break-even cost is the value such that expected_payout - c * expected_rerolls = 0

Examples

Example 1

Input: reroll_game_analysis(0.5, 3.5)

Output: [7, 7]

Example 2

Input: reroll_game_analysis(0.25, 4)

Output: [5.333333333333333, 16]

Example 3

Input: reroll_game_analysis(0.8, 3.2)

Output: [16.000000000000004, 4]

Hints

Hint 1

Set up a recursion: if E is the total expected payout, then E = mu + p·E, since with probability p you restart the entire process after collecting mu.

Hint 2

The expected number of rolls follows a geometric distribution with success probability (1-p). Use this to find how many paid rerolls occur.

Hint 3

For break-even: compute the expected number of rerolls (not total rolls), multiply by cost c, and set net payout to zero.