KatInterview

Weighted Distribution Sampling from Uniform Draws

Heaps, Sorting & SearchAuto-graded · 19 test cases
MediumAuto-gradedHeaps, Sorting & Searcharraysbinary-searchmath

Last updated

Validate probability weights and map pre-generated uniform float values to discrete outcomes using prefix sums and binary search.

Given a list of unique integer items, a list of non-negative numeric weights corresponding to each item, and a sequence of standard uniform numbers $u \in [0, 1]$, sample an item for each standard uniform number according to the normalized cumulative distribution of the weights.

Requirements & Validation

Before generating any samples, validate all inputs. If any of the following checks fail, raise a ValueError:

  1. values and weights must have identical, non-zero lengths.
  2. values must contain only distinct integers (no duplicate elements).
  3. Every weight in weights must be a finite, non-negative number ($w_i \ge 0$).
  4. The sum of all elements in weights, $S = \sum w_i$, must be positive, finite, and within $10^{-9}$ of $1.0$ (that is, $|S - 1.0| \le 10^{-9}$).
  5. Every uniform draw $u \in \text{uniforms}$ must be finite and lie within $[0, 1]$.

Precise Mapping Algorithm

To guarantee exact output matching across floating-point implementations, construct the cumulative prefix array as follows:

  1. Calculate scale = 1.0 / sum(weights).
  2. Construct a cumulative array prefix of length $N = \text{len}(values)$ where:
    • For $i = 0$ to $N-1$, maintain a running float accumulator running. Update running += weights[i] * scale and set prefix[i] = running.
    • Explicitly overwrite prefix[N - 1] = 1.0.
  3. For each value $u \in \text{uniforms}$, find the zero-based outcome index using Python's right binary search algorithm (bisect_right(prefix, u)).
  4. If index == N (which can happen if $u > 1.0$ due to rounding, or $u == 1.0$), clamp index to $N - 1$.
  5. Return the list of mapped values values[index].

Constraints

  • $1 \le \text{len}(values) \le 10^5$
  • $\text{len}(values) == \text{len}(weights)$
  • $0 \le \text{len}(uniforms) \le 10^5$
  • $0.0 \le w_i \le 1.0$
  • $0.0 \le u \le 1.0$

Examples

Example 1

Input: weighted_samples([10, 20, 30], [0.2, 0.3, 0.5], [0, 0.19, 0.2, 0.49, 0.5, 0.99])

Output: [10, 10, 20, 20, 30, 30]

Example 2

Input: weighted_samples([1, 2, 3], [0.5, 0, 0.5], [0.1, 0.5, 0.5000001, 0.999999])

Output: [1, 3, 3, 3]

Example 3

Input: weighted_samples([7, 11], [0.3333333333, 0.6666666667], [0, 0.3333333333, 0.3333333334, 1])

Output: [7, 11, 11, 11]

Hints

Hint 1

Check all input validation constraints first: non-empty matching lengths, unique values, non-negative finite weights, float sum within 1e-9 of 1.0, and uniforms in [0, 1].

Hint 2

Build normalized prefix sums by accumulating weights multiplied by (1.0 / sum(weights)), and set the final entry prefix[-1] explicitly to 1.0.

Hint 3

Use `bisect_right(prefix, u)` to map each uniform float `u` to its target index, clamping the resulting index to len(values) - 1 if it reaches len(values).