The interview problem
A simulator draws millions of categories from an unchanged probability vector. Explain an alias table, build a small example and decide when preprocessing is worthwhile.
Original practice, not a reported company interview. All scenarios and worked numbers are illustrative. Interview date: not applicable. Sources retrieved September 30, 2026.
1. Scope and assumptions
Assume nonnegative finite weights with positive total, independent random draws and a distribution that stays fixed between rebuilds. Constant-time draws do not imply constant-time updates.
2. Worked approach
Normalize n weights and scale each probability by n. Pair an underfull bucket with an overfull bucket: store the underfull threshold and its alias, transfer the missing mass from the larger bucket, and repeat. A draw chooses a bucket uniformly, then a uniform number selects its primary item or alias.
Version the probability vector and table together. Reject invalid values before normalization, define floating-point tolerances and handle zero-mass categories explicitly. Use independent or appropriately generated randomness for bucket and threshold selection. A partially rebuilt table must never be exposed to readers.
3. Calculate the example
For probabilities (0.5, 0.25, 0.25), one valid three-bucket table has bucket 0 always returning A, bucket 1 returning B with probability 0.75 and A otherwise, and bucket 2 returning C with probability 0.75 and A otherwise. Then P(A)=1/3+0.25/3+0.25/3=0.5 and P(B)=P(C)=0.75/3=0.25. This verifies the table without relying on simulation.
4. Failure cases and trade-offs
Updating the original weights without rebuilding produces stale sampling. Negative weights or a zero total do not define a categorical distribution. Rounding can leave a final bucket slightly above or below one; use a tested implementation and verify mass rather than blindly clipping large errors.
Alias preprocessing is O(n) with O(n) memory and O(1) draws. A cumulative distribution supports binary-search draws and is easier to inspect. Frequently changing weights may favor a tree supporting O(log n) updates and draws instead of full rebuilds.
5. Validate the result
Check reconstructed category mass algebraically on small tables, then compare empirical frequencies on large draws with expected sampling variation. Test zeros, one category and highly skewed probabilities. Measure build cost separately from draw throughput.
6. Interview follow-ups and self-review
What happens if only one category has positive weight? How would you publish a new distribution to concurrent readers?
Reward a correct table, mass calculation, update costs and numerical validation.
Sources and related practice
Technical reference: docs.scipy.org — Official alias sampling reference; table and lifecycle example are original.
Exact publication/update date not established in this review. Retrieved September 30, 2026. This reference supports the named technical concept; the proposed workflow and examples are original, not a documented vendor implementation or interview report.
Continue with Negative sampling for retrieval models; Estimate expectations with Monte Carlo.
Continue your preparation
Choose a connected learning path or browse more ML fundamentals questions. Read our editorial method and attribution policy.