Probability Inventory Covers Random Demand
Arrays & HashingAuto-graded · 18 test casesLast updated
Compute the binomial CDF: the chance that at most s of n independent customers request a limited item.
Implement a function demand_covered(stock: int, shoppers: int, request_probability: float) -> float that returns the probability that a stock of stock units is enough for shoppers independent customers.
Each customer, independently of the others, requests the limited item with probability request_probability, and otherwise requests something else that is always available. The stock suffices exactly when the total number of customers requesting the item is less than or equal to stock.
Return the exact probability as a Python float.
Rules and edge cases:
- The number of item requests follows a binomial distribution with
shopperstrials and success probabilityrequest_probability. - If
stock >= shoppers, return1.0(demand can never exceed the number of shoppers). - If
stock < 0orshoppers < 0, return0.0. - Customers are interchangeable; arrival order is irrelevant.
Constraints:
0 <= shoppers <= 1000-1 <= stock <= 10000.0 <= request_probability <= 1.0- Return value lies in
[0.0, 1.0] - Answers are checked with a tolerance of
1e-9
Examples
Example 1
Input: demand_covered(2, 5, 0.3)
Output: 0.8369199999999998
Hints
Hint 1
Let X count how many of the shoppers request the item; X follows Binomial(shoppers, request_probability).
Hint 2
The stock suffices when X <= stock, so sum P(X = k) for k from 0 to stock.
Hint 3
Each term is comb(shoppers, k) * p**k * (1-p)**(shoppers-k); handle the shortcuts when stock >= shoppers or when inputs are negative.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Let X be the number of shoppers who request the limited item. Since each shopper acts independently with the same probability p = request_probability, X follows a binomial distribution with n = shoppers trials. The stock covers everyone exactly when X <= stock, so we want the binomial CDF evaluated at stock:
P(X <= stock) = sum over k = 0..stock of C(n, k) * p^k * (1-p)^(n-k)
We iterate over the relevant values of k and add up the terms, using math.comb for the binomial coefficients.
Shortcuts handle the edges:
- If
stock >= shoppers, even everyone requesting the item uses at mostshoppersunits, so the probability is1.0. - If
stockorshoppersis negative, return0.0.
Why it is correct
The event "the stock is enough" is identical to "the count of item requests is at most stock". Because requests are independent with identical probability, the count is binomial, and the binomial probability mass function gives each count's probability. Summing the masses for all counts 0..stock yields exactly the probability of the event. The edge-case shortcuts are consistent with the same formula.
Complexity
- Time: O(stock) terms, each computed in O(1) apart from big-number arithmetic, so O(stock).
- Space: O(1) extra.
from math import comb
def demand_covered(stock: int, shoppers: int, request_probability: float) -> float:
if stock < 0 or shoppers < 0:
return 0.0
if stock >= shoppers:
return 1.0
p = request_probability
return sum(
comb(shoppers, k) * (p ** k) * ((1.0 - p) ** (shoppers - k))
for k in range(stock + 1)
)