Random Assignment Statistics with Replacement
Arrays & HashingAuto-graded · 25 test casesLast updated
Compute expected allocation count, reachability probability, and expected coverage when distributing items randomly with replacement.
You are distributing items randomly among recipients. Each item is independently assigned to one of the recipients with equal probability, and the same recipient may receive multiple items (sampling with replacement).
Write a function that takes two integers:
n_recipients: the total number of recipients availablen_items: the total number of items to distribute
Return a list of exactly three floating-point values:
- Expected items per recipient: The expected number of items that any particular recipient will receive
- Probability of receiving at least one: The probability that a specific recipient receives one or more items
- Expected coverage: The expected number of recipients who receive at least one item
Constraints
1 ≤ n_recipients ≤ 10^90 ≤ n_items ≤ 10^9- Return values should be accurate to at least 6 decimal places
- Handle edge cases: if no items are distributed, all three values should be 0
Examples
Example 1
Input: random_assignment_stats(10, 100)
Output: [10, 0.9999734386011124, 9.999734386011124]
Example 2
Input: random_assignment_stats(2, 1)
Output: [0.5, 0.5, 1]
Example 3
Input: random_assignment_stats(5, 0)
Output: [0, 0, 0]
Hints
Hint 1
Model each item assignment as an independent Bernoulli trial with success probability 1/n_recipients
Hint 2
For the probability of receiving at least one item, use the complement rule: calculate the probability of receiving zero items
Hint 3
Apply linearity of expectation to find expected coverage, even though individual recipient indicators are dependent
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem requires computing three related statistics for a random allocation process where each item is independently assigned to one of n recipients uniformly at random.
Model the process: Each item assignment is a Bernoulli trial. For a fixed recipient, the probability they receive any particular item is 1/n_recipients. The total number of items that recipient receives follows a Binomial(n_items, 1/n_recipients) distribution.
Part 1 - Expected items per recipient:
By linearity of expectation, if a recipient receives each of n_items items independently with probability 1/n_recipients:
E[items for one recipient] = n_items × (1/n_recipients) = n_items / n_recipients
Alternatively, by symmetry: all n_items items must go somewhere, distributed equally in expectation among n_recipients recipients.
Part 2 - Probability of receiving at least one:
Use the complement rule. A recipient receives zero items only if all n_items assignments miss them:
P(receives 0) = (1 - 1/n_recipients)^n_items
P(receives ≥ 1) = 1 - (1 - 1/n_recipients)^n_items
For numerical stability with large values, we use the identity 1 - (1-p)^k = -expm1(k × log1p(-p)), which avoids catastrophic cancellation.
Part 3 - Expected coverage:
Let I_i be the indicator that recipient i receives at least one item. The total coverage is R = Σ I_i.
By linearity of expectation (which holds even though the indicators are dependent):
E[R] = Σ E[I_i] = Σ P(recipient i receives ≥ 1) = n_recipients × P(one recipient receives ≥ 1)
The key insight is that linearity of expectation does not require independence. While the events "recipient i is reached" and "recipient j is reached" are negatively correlated (if one gets many items, others get fewer), we can still sum the individual probabilities.
Edge cases:
- When n_items = 0, no items are distributed, so all three values are 0
- When n_recipients = 1, the single recipient gets all items with certainty (if any exist)
Complexity
- Time: O(1) - constant number of arithmetic operations and logarithm computations
- Space: O(1) - fixed output list of three values
Solution
import math
from typing import List
def random_assignment_stats(n_recipients: int, n_items: int) -> List[float]:
expected_per_recipient = n_items / n_recipients
if n_recipients == 1:
prob_at_least_one = 1.0 if n_items > 0 else 0.0
else:
prob_at_least_one = -math.expm1(n_items * math.log1p(-1.0 / n_recipients))
expected_coverage = n_recipients * prob_at_least_one
return [expected_per_recipient, prob_at_least_one, expected_coverage]