KatInterview

Stop or Pass: Optimal Play in a Two-Player Dice Duel

Math & Bit ManipulationAuto-graded · 2 test cases
HardAuto-gradedMath & Bit Manipulationexpected-valuegame-theoryoptimal-stopping

Last updated

Find the optimal accept/reject rule and the game value for a symmetric alternating-roll zero-sum game, using a stationary fixed-point equation.

Two players, A and B, play a zero-sum game with a fair 21-sided die. Its faces show the integers from -10 to 10, one per face, and every face has probability 1/21. Rolls are independent.

A rolls first. When it is your turn, you roll the die, see the face value v, and then choose one of two actions:

  • Accept: the game ends at once and your opponent pays you v. If v is negative, you pay your opponent |v|.
  • Reject: nobody pays anything and the opponent takes the next turn, rolling and facing the same choice.

Turns alternate until someone accepts. Both players are risk-neutral and maximize their expected payoff. Both know the other plays optimally. There is no discounting, no cost for rejecting, and no limit on the number of turns.

Your task is to write a function that takes no arguments and returns the optimal strategy of each player together with the expected payoff to A.

Return value. A dictionary with exactly three keys:

  • "accept": a dictionary with keys "A" and "B". Each maps to an ascending list of integers.
  • "reject": a dictionary with keys "A" and "B", in the same format.
  • "expected_payoff_to_A": a float, the expected payoff to A under optimal play by both.

How to express each player's lists. Both lists are written from A's point of view, as the amount A receives if the game ends on that roll.

  • For A, the numbers are the faces A rolls. Face v is in "accept" if A should accept it, and in "reject" otherwise.
  • For B, the numbers are the payoffs to A caused by B's roll. If B rolls face w, the corresponding number is -w. The number -w is in B's "accept" list if B should accept w, and in B's "reject" list otherwise.

Together, each player's accept and reject lists must cover all 21 values from -10 to 10 exactly once.

If a player would be exactly indifferent between accepting and rejecting on some face, that cannot be the case for any face in this game, so no tie-break rule is needed.

Constraints

  • The die has exactly the 21 faces -10, -9, ..., 10, each with probability 1/21.
  • Rolls are independent and the game is zero-sum.
  • Both players are risk-neutral expected-value maximizers.
  • The function takes no parameters.
  • Every list in the returned dictionary is sorted in ascending order.
  • expected_payoff_to_A is the exact game value as a float.

Examples

Example 1

Input: alternating_die_policy()

Output: {'accept': {'A': [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'B': [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1]}, 'reject': {'A': [-10, -9, -8, -7, -6, -5, -4, -3, -2], 'B': [2, 3, 4, 5, 6, 7, 8, 9, 10]}, 'expected_payoff_to_A': 1.8}

Hints

Hint 1

Nothing separates turn 1 from turn 7 except who is rolling, so look for a single value V for 'the player about to roll'. Rejecting hands the opponent a fresh roll worth V to them, which is worth -V to you.

Hint 2

This gives the equation V = E[max(v, -V)]. You accept a face v exactly when v >= -V. Since max(v, -V) >= v, V is at least 0, so the threshold -V is at most 0.

Hint 3

Suppose you accept all faces from -k up to 10. Solve the resulting linear equation for V and check that -V falls between the accepted and rejected faces. Only one k is consistent. For B, flip the sign of each face to express it from A's side.