Bayesian Guessing Game with Unknown Coin Bias
Arrays & HashingAuto-graded · 24 test casesLast updated
Compute expected optimal winnings when sequentially guessing coin flips with a uniform-prior unknown bias.
A coin is flipped n times in a row. The probability of Heads is an unknown value p; before any flips occur, p is drawn once from a Uniform(0, 1) distribution and then stays fixed for the entire sequence.
Before each flip you must commit to guessing either Heads or Tails, and you see the actual outcome only after your guess. Every correct guess earns you $1; every incorrect guess earns you $0.
You may choose each guess adaptively, based on the outcomes of all previous flips (and therefore on your running count of Heads and Tails).
Given n, return the maximum possible expected total winnings over the whole sequence of n flips, when you follow the guessing policy that maximizes expected total reward. Report the answer as a float.
Task: implement a function that, given n, returns that maximum expected total winnings.
Constraints
1 <= n <= 1000pis drawn once fromUniform(0, 1)and is fixed for allnflips (not re-drawn per flip).- Guesses may depend on all previously observed outcomes.
- A correct guess pays exactly
1, an incorrect guess pays exactly0. - Return value is a floating-point number (no specific precision required beyond what the reference produces).
Examples
Example 1
Input: max_expected_winnings(1)
Output: 0.5
Example 2
Input: max_expected_winnings(2)
Output: 1.1666666666666665
Example 3
Input: max_expected_winnings(10)
Output: 6.833621933621934
Example 4
Input: max_expected_winnings(100)
Output: 73.77863732823776
Hints
Hint 1
With a Uniform(0,1) prior, after seeing h heads and t-h tails, the posterior predictive probability of Heads is (h+1)/(t+2).
Hint 2
Before any flip the total number of Heads is equally likely to be any value in {0, 1, ..., t}, so you can average the best achievable one-step reward over that uniform distribution.
Hint 3
At step t the optimal expected reward is the larger of the two posterior-predictive probabilities; sum that over all steps.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Because the prior on p is Uniform(0, 1), it is conjugate to the Bernoulli likelihood: after observing h heads and m tails, the posterior is Beta(h+1, m+1). The posterior-predictive probability that the next flip is Heads is (h+1)/(h+m+2). Since a correct guess pays 1 and an incorrect pays 0, the optimal action for a single flip is simply to guess whichever outcome has the larger posterior-predictive probability. This greedy choice is also globally optimal because each guess only affects its own reward, and the posterior-predictive probability already summarizes everything observed so far.
To get the expected total over the random sequence, note that with a Uniform prior the number of heads after t flips is uniform on {0, 1, ..., t}. So at step t (0-indexed), for each possible head count h you add max(h+1, t-h+1) weighted by 1/((t+1)(t+2)) — the probability of exactly h heads among t flips under the uniform prior. Sum over all t from 0 to n-1.
Why it is correct
Each per-flip guess earns its posterior-predictive maximum in expectation, and the exchangeability of the uniform-prior model makes the count of heads after t flips uniform, so averaging the optimal one-step reward over that uniform distribution gives the exact expected optimal winnings for that step. Summing over independent steps yields the total.
Complexity
The nested loop is O(n²) time and O(1) extra space.
from typing import List
def max_expected_winnings(n: int) -> float:
total = 0.0
for t in range(n):
best = 0
for h in range(t + 1):
best += max(h + 1, t - h + 1)
total += best / ((t + 1) * (t + 2))
return total