Maximize Expected Return in Two Opaque Containers Game
Math & Bit ManipulationAuto-graded · 20 test casesLast 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:
- Place: A unit of currency is added to one of the two containers, chosen uniformly at random ($p = 0.5$ for each container).
- 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 lengthturns."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]}
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
- Deterministic Strategy: Because no intermediate observations are available, the optimal policy must be a predetermined fixed sequence of moves.
- Structural Reduction: Consider any sequence containing a
Takemove immediately preceding aPlacemove (TP). Swapping these adjacent moves toPTincreases (or keeps identical) the number of subsequentTakeevents for every placed coin, strictly increasing the probability of collecting each coin. Therefore, the optimal sequence always groups allPlacemoves first, followed by allTakemoves. - Probability Calculation: If there are $p$
Placemoves followed by $t$Takemoves (where $p + t = \text{turns}$):- A specific coin is placed in one of the two boxes.
- Each
Takeaction independently selects the correct box with probability $1/2$. - The probability that the coin remains uncollected after $t$
Takeactions is $(1/2)^t$. - The probability that the coin is collected is $1 - (1/2)^t = \frac{2^t - 1}{2^t}$.
- By linearity of expectation, the overall expected payoff is $p \cdot \frac{2^t - 1}{2^t}$.
- Optimization: We can evaluate $t \in [0, \text{turns}]$ and select the value of $t$ that yields the maximum payoff. Exact rational arithmetic (
Fraction) is used to maintain precision and avoid floating-point issues.
Complexity
- Time Complexity: $O(\text{turns})$ iterations, where each step does basic arithmetic operations. For $\text{turns} \le 1000$, this runs in under a millisecond.
- Space Complexity: $O(\text{turns})$ to construct the strategy output list.
from fractions import Fraction
from typing import Dict, Any
def optimal_box_game(turns: int) -> Dict[str, Any]:
best_value = Fraction(-1, 1)
best_takes = 0
for takes in range(turns + 1):
places = turns - takes
value = Fraction(places * (2 ** takes - 1), 2 ** takes)
if value > best_value:
best_value = value
best_takes = takes
places = turns - best_takes
return {
"place_turns": places,
"take_turns": best_takes,
"strategy": ["Place"] * places + ["Take"] * best_takes,
"expected_payoff": [best_value.numerator, best_value.denominator],
}