Katkat

How to Design a Proportional Randomized Sampler?

PinterestData Scientist · Onsite

Last updated

This question evaluates proficiency in randomized algorithms and probabilistic sampling, with emphasis on numerical precision, large-weight handling, and memory-versus-accuracy trade-offs. Commonly asked in Coding & Algorithms interviews for data-science roles, it assesses practical application ability grounded in conceptual understanding of numerical stability and relevant data-structure choices.

Scenario

Randomized promotion engine must pick an item proportional to its score, but scores have no upper bound.

Question

Design a sampler pick() that returns an item with probability proportional to its (possibly very large) weight. Discuss memory and precision trade-offs when weights exceed 32-bit limits.

Hints

Prefix-sum array + binary search or alias table; rescale or use long/double for huge weights.