Simulate Coin Flips to Determine Fairness via Empirical Distribution
Company: Google
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates a candidate's ability to perform simulation-based hypothesis testing by implementing Monte Carlo sampling and computing an empirical two-sided p-value from observed coin-flip counts, emphasizing statistical computation and numerical accuracy.
Constraints
- 0 <= len(simulated_counts) <= 10^6
- 0 <= simulated_counts[i] <= n
- 0 <= observed <= n
- 1 <= n
- Return 0.0 when simulated_counts is empty.
- Two-sided extremeness is measured by absolute deviation from the mean n/2; use >= so the observed value's own deviation counts as extreme.
Examples
Input: ([4, 5, 6, 5, 4, 6, 3, 7, 5, 5], 8, 10)
Expected Output: 0.0
Explanation: mean=5, observed deviation=|8-5|=3. No simulated count deviates by 3 or more (max deviation here is 2), so the p-value is 0/10 = 0.0.
Input: ([5, 5, 5, 5], 5, 10)
Expected Output: 1.0
Explanation: observed=5 equals the mean, so observed deviation=0. Every count's deviation (0) is >= 0, giving 4/4 = 1.0.
Hints
- The expected number of heads for a fair coin is n/2. 'Extreme' means far from this mean in EITHER direction, so measure abs(count - n/2).
- The two-sided p-value is the proportion of simulated trials whose deviation from n/2 is greater than OR EQUAL to the observed deviation.
- Use >= (not >) at the boundary so that simulated trials exactly as extreme as the observed count are included.
- A single linear scan over simulated_counts is enough — no sorting or closed-form binomial formula is needed.