Binomial Probability of Exactly k Successes
Arrays & HashingAuto-graded · 24 test casesLast updated
Tests whether a candidate can implement the binomial PMF, including the combination factor, a bias parameter, and out-of-range edge cases.
Binomial Probability of Exactly k Successes
You are running a series of n independent trials. On each trial the outcome is either a success or a failure. The probability of success on any single trial is p, and this value stays the same across all trials.
Given n, k, and p, return the probability that the number of successes equals exactly k.
Background
Because every trial is independent, the probability of one particular ordering that contains exactly k successes and n − k failures is:
p^k × (1 − p)^(n − k)
However, there are many orderings that produce the same count of k successes. The number of ways to choose which k of the n trials are successes is the binomial coefficient:
C(n, k) = n! / (k! × (n − k)!)
Multiplying these two quantities gives the binomial probability mass function (PMF):
P(X = k) = C(n, k) × p^k × (1 − p)^(n − k)
Task
Implement a function that evaluates this formula for the given inputs and returns the result as a float.
If k is outside the range [0, n], return 0.0.
Constraints
1 ≤ n ≤ 10000 ≤ k ≤ n(the function must also handle values ofkoutside this range gracefully)0.0 ≤ p ≤ 1.0- The answer will fit in a standard 64-bit float; no special big-number handling is required.
- Do not use any statistical library functions (e.g.,
scipy.stats.binom.pmf); compute the result directly from the formula.
Examples
Example 1
Input: binomial_pmf(5, 2, 0.5)
Output: 0.3125
Example 2
Input: binomial_pmf(5, 2, 0.7)
Output: 0.13230000000000006
Example 3
Input: binomial_pmf(10, 0, 0.3)
Output: 0.028247524899999984
Example 4
Input: binomial_pmf(10, 10, 0.3)
Output: 0.0000059048999999999975
Hints
Hint 1
The binomial coefficient C(n, k) counts the number of distinct orderings of k successes among n trials — Python's math.comb(n, k) computes this exactly with integers, avoiding floating-point rounding in that step.
Hint 2
Check whether k is negative or greater than n before doing any arithmetic; both cases have zero valid orderings and should return 0.0 immediately.
Hint 3
The full formula is just three factors multiplied together: the combination count, p raised to the power k, and (1 − p) raised to the power (n − k). Assemble them in that order and you are done.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The binomial PMF has three components that are simply multiplied together.
1. Validate the input.
A trial sequence cannot have fewer than zero successes or more successes than trials, so any k outside [0, n] corresponds to an impossible event. Return 0.0 immediately in those cases.
2. Count the orderings.
C(n, k) counts the number of distinct ways to arrange k successes among n positions. math.comb computes this with exact integer arithmetic, which avoids unnecessary floating-point error in this step.
3. Compute the per-ordering probability.
One specific sequence of k successes and n − k failures has probability p^k × (1 − p)^(n − k), because the trials are independent and each contributes either p or (1 − p).
4. Multiply.
Since every valid ordering is mutually exclusive and has the same probability, the total is C(n, k) × p^k × (1 − p)^(n − k).
Why it is correct
The result is a well-known closed-form expression — the binomial PMF — and can be verified by two properties:
- It sums to 1 over all
kfrom 0 ton(by the binomial theorem:(p + (1 − p))^n = 1). - For
p = 0.5andn = 10,k = 5, it gives252 / 1024 ≈ 0.246, which matches manual enumeration.
Complexity
- Time: O(n) —
math.comb(n, k)runs in O(min(k, n − k)) multiplications, and the two power operations are O(k) and O(n − k) respectively, so the whole function is O(n) in the worst case. - Space: O(1) — no auxiliary data structures are used.
Reference solution
from math import comb
def binomial_pmf(n: int, k: int, p: float) -> float:
if k < 0 or k > n:
return 0.0
return comb(n, k) * (p ** k) * ((1 - p) ** (n - k))