Survey Gender Distribution Probability
Arrays & HashingAuto-graded · 21 test casesLast updated
Compute probabilities of observing specific gender counts in survey samples under binomial and hypergeometric models.
You are conducting a survey where each respondent reports their gender. You need to calculate the probability of observing a particular gender distribution in your sample under two different sampling models.
Given:
- A sample of 70 respondents
- Exactly 30 female respondents observed
- A population parameter
p(the true proportion of females in the population) - A finite population of size
NcontainingFfemales
Calculate the probability of observing exactly 30 females out of 70 respondents under two scenarios:
-
Independent sampling (binomial model): Each respondent is drawn independently from an infinite population where the probability of selecting a female is
p. -
Sampling without replacement (hypergeometric model): Respondents are drawn without replacement from a finite population of size
Ncontaining exactlyFfemales.
Return both probabilities in a dictionary with keys "binomial" and "hypergeometric".
Constraints
0 ≤ p ≤ 1N ≥ 70(population size must be at least as large as the sample)0 ≤ F ≤ N(number of females cannot exceed population size)- The sample size is always 70
- The observed female count is always 30
- If the hypergeometric distribution is undefined (insufficient females or males in the population), return 0.0 for that probability
Examples
Example 1
Input: survey_gender_probability(0.5, 1000, 500)
Output: {'binomial': 0.046881359385314195, 'hypergeometric': 0.04607082868542493}
Example 2
Input: survey_gender_probability(0.4, 500, 200)
Output: {'binomial': 0.08530012110984632, 'hypergeometric': 0.09038514773415597}
Example 3
Input: survey_gender_probability(0.6, 100, 60)
Output: {'binomial': 0.001479234602050546, 'hypergeometric': 4.026392935773107e-9}
Hints
Hint 1
For independent sampling with a fixed probability, which discrete distribution describes the count of successes in n trials?
Hint 2
For sampling without replacement from a finite population, the hypergeometric distribution applies—what happens when the population doesn't contain enough individuals of each type?
Hint 3
Use the combination formula C(n,k) = n!/(k!(n-k)!) and ensure you handle edge cases where the required count exceeds availability
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem requires computing probabilities under two different statistical models for sampling:
Binomial Model (Independent Sampling): When each respondent is sampled independently with replacement (or from an effectively infinite population), the number of females follows a binomial distribution. The probability of observing exactly k successes in n trials is:
P(X = k) = C(n, k) × p^k × (1-p)^(n-k)
Where C(n, k) is the binomial coefficient "n choose k".
Hypergeometric Model (Sampling Without Replacement): When sampling without replacement from a finite population of size N containing F females, the count follows a hypergeometric distribution:
P(X = k) = [C(F, k) × C(N-F, n-k)] / C(N, n)
This represents choosing k females from F available and (n-k) males from (N-F) available, divided by all possible ways to choose n individuals from N.
Edge Cases: The hypergeometric probability is undefined (or zero) when the population doesn't contain enough individuals of each gender to satisfy the observed sample. Specifically, if F < 30 or (N - F) < 40, we cannot draw 30 females and 40 males, so the probability is 0.
Complexity
- Time: O(1) - Fixed number of combination calculations
- Space: O(1) - Only storing the two probability values
The combination function math.comb is optimized and handles large numbers efficiently.
import math
from typing import Dict
def survey_gender_probability(p: float, N: int, F: int) -> Dict[str, float]:
sample_size = 70
females_observed = 30
# Binomial probability: P(X = k) = C(n,k) * p^k * (1-p)^(n-k)
binomial_prob = (
math.comb(sample_size, females_observed)
* (p ** females_observed)
* ((1 - p) ** (sample_size - females_observed))
)
# Hypergeometric probability: P(X = k) = C(F,k) * C(N-F,n-k) / C(N,n)
# Check if we have enough females and males in the population
males_in_population = N - F
males_needed = sample_size - females_observed
if F < females_observed or males_in_population < males_needed:
hypergeometric_prob = 0.0
else:
hypergeometric_prob = (
math.comb(F, females_observed)
* math.comb(males_in_population, males_needed)
/ math.comb(N, sample_size)
)
return {
"binomial": binomial_prob,
"hypergeometric": hypergeometric_prob
}