KatInterview

Maximize Expected Return in Two Opaque Containers Game

Math & Bit ManipulationAuto-graded · 20 test cases
MediumAuto-gradedMath & Bit Manipulationprobabilityexpected-valuegreedy

Last updated

Find the optimal sequence of deposit and collection moves to maximize expected value in an unobserved two-container game.

Consider a game played over turns rounds involving two unobserved containers that are initially empty. In each round, you must perform one of two actions:

  1. Place: A unit of currency is added to one of the two containers, chosen uniformly at random ($p = 0.5$ for each container).
  2. Take: One of the two containers is chosen uniformly at random ($p = 0.5$), all contents currently in that container are retrieved by you, and the container is left empty.

You receive no feedback during the game (you cannot inspect container contents or know when currency is successfully collected until all rounds finish). Consequently, your action plan must be fixed in advance.

To maximize expected total collection, determine:

  • The optimal strategy, which consists of performing all Place moves first followed by all Take moves.
  • The number of Place turns and Take turns.
  • The exact maximum expected payoff, represented as an irreducible fraction $[N, D]$ (where $N / D$ is in lowest terms). If there are multiple choices for the number of Take turns that achieve the exact same maximum expected payoff, choose the one with the smallest number of Take turns.

Inputs

  • turns: An integer representing the total number of moves available ($0 \le \text{turns} \le 1000$).

Outputs

Return a dictionary with four key-value pairs:

  • "place_turns": int — number of Place actions.
  • "take_turns": int — number of Take actions.
  • "strategy": List[str] — sequence of string actions, e.g., ["Place", ..., "Take", ...] of length turns.
  • "expected_payoff": List[int] — a list [numerator, denominator] representing the exact expected value in lowest terms.

Constraints

  • $0 \le \text{turns} \le 1000$

Examples

Example 1

Input: optimal_box_game(100)

Output: {'place_turns': 94, 'take_turns': 6, 'strategy': ['Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Place', 'Take', 'Take', 'Take', 'Take', 'Take', 'Take'], 'expected_payoff': [2961, 32]}