Getting a fair result with an unfair coin

How can you get a fair coin toss if someone hands you a coin that is weighted to come up heads more often than tails?

šŸ’”Strategies for Solving This Problem

The Classic Probability Puzzle

This came up in my Google interview. It's not about coding - it's about thinking probabilistically. But you can implement it, which is what they asked me to do.

The Problem

You have a biased coin (unknown probability p of heads). How do you generate a fair 50/50 result?

The Insight (von Neumann's Trick)

Flip the coin twice:

  • HT → return "heads" (probability: p Ɨ (1-p))
  • TH → return "tails" (probability: (1-p) Ɨ p)
  • HH or TT → try again

Since p Ɨ (1-p) = (1-p) Ɨ p, the outcomes HT and TH are equally likely!

Why It Works

Even though the coin is biased, the probability of HT equals the probability of TH. By mapping HT to "heads" and TH to "tails", we get a fair result.

Expected flips: 1/(2p(1-p)). For p=0.5 (fair coin), this is 2 flips. For extreme bias (p near 0 or 1), it could be many flips.

What My Interviewer Asked

  1. "Explain why it works" (probability argument)
  2. "What if the coin lands the same both times?" (retry)
  3. "What's the expected number of flips?" (depends on p)
  4. "Implement it" (code below)
Scroll to Top