KatInterview

Random Assignment Statistics with Replacement

Arrays & HashingAuto-graded · 25 test cases
EasyAuto-gradedArrays & Hashingprobabilityexpectationbinomial

Last 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 available
  • n_items: the total number of items to distribute

Return a list of exactly three floating-point values:

  1. Expected items per recipient: The expected number of items that any particular recipient will receive
  2. Probability of receiving at least one: The probability that a specific recipient receives one or more items
  3. Expected coverage: The expected number of recipients who receive at least one item

Constraints

  • 1 ≤ n_recipients ≤ 10^9
  • 0 ≤ 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