KatInterview

Survey Gender Distribution Probability

Arrays & HashingAuto-graded · 21 test cases
HardAuto-gradedArrays & Hashingprobabilitystatisticscombinatorics

Last 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 N containing F females

Calculate the probability of observing exactly 30 females out of 70 respondents under two scenarios:

  1. Independent sampling (binomial model): Each respondent is drawn independently from an infinite population where the probability of selecting a female is p.

  2. Sampling without replacement (hypergeometric model): Respondents are drawn without replacement from a finite population of size N containing exactly F females.

Return both probabilities in a dictionary with keys "binomial" and "hypergeometric".

Constraints

  • 0 ≤ p ≤ 1
  • N ≥ 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