KatInterview

Bayesian Coin Identification from Flip Observations

Arrays & HashingAuto-graded · 21 test cases
HardAuto-gradedArrays & Hashingbayes-theoremprobabilityfractions

Last updated

Compute exact posterior probabilities over three weighted coins from a sequence of flips, plus the predictive probability the next flip is heads.

A bag holds three coins that look identical but have different head probabilities:

  • Coin "A": P(H) = 1/2
  • Coin "B": P(H) = 4/5
  • Coin "C": P(H) = 1

One coin is picked uniformly at random (each with prior probability 1/3), and then it is flipped repeatedly. You are given the sequence of results.

Implement coin_beliefs(observations) that returns each coin's posterior probability after observing the sequence, plus the probability that one more flip of the same coin lands heads.

Inputs and outputs:

  • observations is a list of strings, each either "H" or "T". Any other entry must raise ValueError.
  • Flips are conditionally independent given the coin.

Return a dictionary with exactly these keys:

  • "coin_c_fraction": the posterior probability of coin "C" as a string "numerator/denominator" in lowest terms.
  • "coin_c_decimal": that same probability as a float rounded to 4 decimal places.
  • "posterior_fractions": a dict mapping "A", "B", "C" to the posterior probability of each coin, as reduced fraction strings.
  • "posterior_decimals": a dict mapping "A", "B", "C" to the same posteriors rounded to 4 decimal places as floats.
  • "next_heads_fraction": the probability the next flip is heads, given the observations, as a reduced fraction string. This is computed by averaging each coin's head probability over the posterior.
  • "next_heads_decimal": that value rounded to 4 decimal places.

Rules and edge cases:

  • Each posterior is the coin's likelihood (the probability of the observed sequence under that coin) times its prior, divided by the total likelihood. Since all priors are equal, the common factor cancels, but the returned probabilities must still be normalized to sum to 1.
  • A posterior of zero is returned as "0/1" and 0.0.
  • If the sequence has zero probability under all three coins (for example a sequence containing flips impossible under every coin — impossible here since only "T" under coin "C" has zero probability but every sequence with a "T" is still possible under "A"), raise ValueError. In practice the only way to raise is an invalid symbol.
  • Use exact rational arithmetic (for example Python's fractions.Fraction) so fractions are exact and only the final decimals are rounded.

Examples

Example 1

Input: coin_beliefs(['H', 'H', 'H'])

Output: {'coin_c_fraction': '1000/1637', 'coin_c_decimal': 0.6109, 'posterior_fractions': {'A': '125/1637', 'B': '512/1637', 'C': '1000/1637'}, 'posterior_decimals': {'A': 0.0764, 'B': 0.3128, 'C': 0.6109}, 'next_heads_fraction': '14721/16370', 'next_heads_decimal': 0.8993}

Hints

Hint 1

The posterior for each coin is proportional to its prior times the likelihood of the whole sequence under that coin; with equal priors it is just the likelihood divided by the sum of likelihoods.

Hint 2

The likelihood of a sequence under a coin is the product over flips of p for heads and (1-p) for tails; track it exactly with fractions.

Hint 3

The next-flip probability is not any single coin's head rate: average each coin's head probability weighted by its posterior.