Calculate Binomial Cumulative Probability for Limited Supply
Math & Bit ManipulationAuto-graded · 19 test casesLast updated
Tests understanding of binomial distributions and computing cumulative probabilities for discrete independent events.
You are modeling demand at a service counter where each of n visitors independently decides to make a purchase with probability p. The counter has limited stock and can fulfill at most 2 purchase requests.
Write a function that calculates the probability that the counter can satisfy all purchase requests. In other words, compute the probability that the number of visitors who decide to purchase is at most 2.
Assume purchase decisions follow a binomial distribution with parameters n (number of visitors) and p (probability each visitor makes a purchase).
Task
Given:
n: the total number of visitors (integer)p: the probability that any single visitor makes a purchase (float between 0 and 1)
Return the probability that 0, 1, or 2 visitors make a purchase.
Constraints
0 <= n <= 10000.0 <= p <= 1.0- Return the result as a floating-point number
- When
n < 2, the counter can always satisfy demand (all outcomes have at mostnpurchases)
Examples
Example 1
Input: binomial_prob_at_most_two(5, 0.5)
Output: 0.5
Example 2
Input: binomial_prob_at_most_two(2, 0.5)
Output: 1
Example 3
Input: binomial_prob_at_most_two(5, 0.2)
Output: 0.9420800000000003
Example 4
Input: binomial_prob_at_most_two(5, 0.8)
Output: 0.05791999999999997
Hints
Hint 1
The number of purchases follows a binomial distribution B(n, p). You need to sum probabilities for k = 0, 1, and 2 purchases.
Hint 2
The binomial probability mass function is P(X = k) = C(n, k) × p^k × (1 - p)^(n - k), where C(n, k) is the binomial coefficient.
Hint 3
When p = 0.5, the distribution is symmetric around n/2. This can help you verify your answer for special cases.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem requires computing a cumulative binomial probability. When we have n independent trials, each with success probability p, the number of successes follows a binomial distribution B(n, p).
The probability of exactly k successes is given by the binomial probability mass function:
P(X = k) = C(n, k) × p^k × (1 - p)^(n - k)
where C(n, k) is the binomial coefficient "n choose k", equal to n! / (k! × (n - k)!).
To find the probability of at most 2 successes, we sum the probabilities for k = 0, 1, and 2:
P(X ≤ 2) = P(X = 0) + P(X = 1) + P(X = 2)
Edge case: When n < 2, it's impossible to have more than n successes, so we only sum up to min(2, n).
Why This Works
The binomial distribution models independent trials perfectly. Each visitor's decision is independent with the same probability p, which matches the binomial assumptions. Since the events are mutually exclusive (you can't have both exactly 0 and exactly 1 purchase), we add their probabilities to get the cumulative probability.
Complexity
- Time: O(1) since we compute at most 3 terms (k = 0, 1, 2) regardless of n. Each term involves computing a binomial coefficient in O(k) = O(1) time.
- Space: O(1) as we only store intermediate calculation results.
Implementation
from math import comb
def binomial_prob_at_most_two(n: int, p: float) -> float:
"""
Calculate the probability that at most 2 out of n independent events occur,
where each event has probability p.
Args:
n: Number of independent trials
p: Probability of success for each trial
Returns:
Probability that 0, 1, or 2 successes occur
"""
upper_limit = min(2, n)
return sum(comb(n, k) * (p ** k) * ((1 - p) ** (n - k)) for k in range(upper_limit + 1))