Head Counts in n Fair Coin Tosses: Sequences and Tail Odds
Math & Bit ManipulationAuto-graded · 22 test casesLast updated
Compute the number of sequences, the chance of exactly 3 heads, and the chance of at most 3 heads for n fair coin tosses, using binomial counting and handling small n.
Fair Coin Tosses: Counting and Probabilities
A fair coin is tossed n times. Each toss is independent, and heads and tails are equally likely. Write a function that returns three values describing the experiment.
Task
Given a non-negative integer n, return a dictionary with exactly these keys:
"total_sequences": the number of distinct ordered sequences of heads and tails of lengthn. This is an integer."exactly_3_heads": the probability that exactly 3 of thentosses are heads."at_most_3_heads": the probability that the number of heads is 0, 1, 2, or 3.
Rules and edge cases
- Every sequence of length
nis equally likely, so a probability is the number of favourable sequences divided bytotal_sequences. - If
n < 3, exactly 3 heads is impossible, so"exactly_3_heads"is0. - If
n <= 3, every outcome has at most 3 heads, so"at_most_3_heads"is1. - For
n = 0there is exactly one sequence, the empty one. - Compute each probability as an exact integer count divided once by
total_sequences. Do not multiply many small floats together, so your values match the expected ones exactly.
Constraints
0 <= n <= 1000nis an integer.- The returned dictionary must contain exactly the three keys above.
Examples
Example 1
Input: fair_coin_head_stats(10)
Output: {'total_sequences': 1024, 'exactly_3_heads': 0.1171875, 'at_most_3_heads': 0.171875}
Example 2
Input: fair_coin_head_stats(3)
Output: {'total_sequences': 8, 'exactly_3_heads': 0.125, 'at_most_3_heads': 1}
Example 3
Input: fair_coin_head_stats(0)
Output: {'total_sequences': 1, 'exactly_3_heads': 0, 'at_most_3_heads': 1}
Example 4
Input: fair_coin_head_stats(12)
Output: {'total_sequences': 4096, 'exactly_3_heads': 0.0537109375, 'at_most_3_heads': 0.072998046875}
Hints
Hint 1
Each toss has 2 outcomes, so the total number of ordered sequences is a power of 2.
Hint 2
The number of sequences with exactly k heads is the number of ways to choose which k positions are heads, a binomial coefficient.
Hint 3
For 'at most 3', add the counts for k = 0, 1, 2, 3, but never go beyond n. Divide the integer count by 2**n only once at the end.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Because the coin is fair and the tosses are independent, all 2^n ordered sequences are equally likely. Each probability is therefore a counting problem:
probability = (number of favourable sequences) / 2^n
- Total sequences: each of the
ntosses has 2 outcomes, so there are2^nsequences. - Exactly 3 heads: choose which 3 of the
npositions are heads, givingC(n, 3)sequences. Whenn < 3this count is 0. - At most 3 heads: the events "exactly k heads" for different
kare mutually exclusive, so the count isC(n,0) + C(n,1) + ... + C(n,min(3,n)). Capping the upper limit atnhandles smalln. Forn <= 3the sum is2^n, so the probability is 1.
Why it is correct
Every sequence with exactly k heads is counted once by C(n, k), and sequences with different head counts never overlap. Dividing by the total 2^n gives the exact probability under equal likelihood. Summing integer counts first and dividing once avoids accumulated floating-point error. For n = 0, the only sequence is the empty one, with 0 heads, so the result is 1, 0, 1.
Complexity
- Time: O(1) binomial evaluations (at most 4), plus big-integer arithmetic on numbers of about
nbits, which is cheap forn <= 1000. - Space: O(1) beyond the big integers involved.
Reference solution
import math
from typing import Dict
def fair_coin_head_stats(n: int) -> Dict[str, float]:
total = 2 ** n
exactly_3 = math.comb(n, 3) / total if n >= 3 else 0.0
at_most_3 = sum(math.comb(n, k) for k in range(min(3, n) + 1)) / total
return {
"total_sequences": total,
"exactly_3_heads": exactly_3,
"at_most_3_heads": at_most_3,
}