Shelf Stock Versus Random Demand in a Checkout Line
Math & Bit ManipulationAuto-graded · 21 test casesLast updated
Compute binomial probabilities that a limited stock covers random demand from a line of customers, both as at most M buyers and as exactly M buyers.
A line of customers shoppers is waiting at a checkout. Every shopper independently picks exactly one of two products. With probability p_scarce they pick the scarce product, and otherwise (probability 1 - p_scarce) they pick the other one, which is always in plentiful supply. Each shopper takes exactly one unit.
The shop has only units copies of the scarce product left. Let X be the number of shoppers who choose the scarce product.
Write a function that returns two probabilities as a dictionary:
"enough": the probability that the stock covers everyone who wants the scarce product, that is,P(X <= units)."exactly_enough": the probability that the stock is used up exactly, that is,P(X = units).
Details
Xfollows a binomial distribution withcustomerstrials and success probabilityp_scarce.- If
units >= customers, then"enough"is1.0, because demand can never exceed the line length. - If
units > customers, then"exactly_enough"is0.0, because demand can never reachunits. - Return a dictionary with exactly the keys
"enough"and"exactly_enough", each mapped to a float. - Floating-point results are expected. Compute directly from the binomial formula, using
1.0 - p_scarcefor the complementary probability.
Constraints
customersis a positive integer.unitsis a nonnegative integer.0 < p_scarce < 1.- Each shopper's choice is independent of the others and of the remaining stock.
Examples
Example 1
Input: shelf_stock_probabilities(5, 2, 0.4)
Output: {'enough': 0.68256, 'exactly_enough': 0.3456}
Example 2
Input: shelf_stock_probabilities(3, 0, 0.7)
Output: {'enough': 0.027000000000000014, 'exactly_enough': 0.027000000000000014}
Example 3
Input: shelf_stock_probabilities(4, 4, 0.25)
Output: {'enough': 1, 'exactly_enough': 0.00390625}
Hints
Hint 1
The number of shoppers who want the scarce product is a sum of independent yes/no choices. Which standard distribution is that?
Hint 2
The probability of exactly k buyers is C(n, k) * p^k * (1-p)^(n-k). The 'enough' event is the sum of this over k from 0 to units.
Hint 3
Handle units >= customers separately: 'enough' is then 1.0, and 'exactly_enough' is 0.0 when units is strictly greater than customers.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Each shopper independently wants the scarce product with probability p, so the demand X is Binomial(n, p), where n is the number of customers. The probability of exactly k buyers is
P(X = k) = C(n, k) * p^k * (1 - p)^(n - k).
- The stock is "exactly enough" when
X = m, so we evaluate the formula once atk = m. Ifm > n, this is impossible and the answer is0.0. - The stock is "enough" when
X <= m, so we sum the formula overk = 0..m. Ifm >= n, every outcome qualifies and the answer is1.0, so we skip the sum.
Why it is correct
The binomial model follows directly from independence and identical probabilities across shoppers. The events X = k for different k are disjoint, so the probability of X <= m is the sum of the individual terms. The edge cases follow because X can only take values from 0 to n.
Complexity
Time is O(m) binomial terms, at most O(n), with each term costing a comb call and a few powers. Space is O(1) beyond the returned dictionary.
Reference solution
import math
from typing import Dict
def shelf_stock_probabilities(customers: int, units: int, p_scarce: float) -> Dict[str, float]:
n, m, p = customers, units, p_scarce
# Demand for the scarce product is Binomial(n, p).
if m >= n:
enough = 1.0
else:
q = 1.0 - p
enough = sum(
math.comb(n, k) * (p ** k) * (q ** (n - k))
for k in range(m + 1)
)
if m <= n:
exact = math.comb(n, m) * (p ** m) * ((1.0 - p) ** (n - m))
else:
exact = 0.0
return {"enough": enough, "exactly_enough": exact}