The 60-second answer
For static weights, prefix sums plus binary search give O(n) build/O(log n) sample; alias tables give O(n) build/O(1) sample. For frequent point updates, a Fenwick or segment tree stores subtree weight mass and supports both update and sample in O(log n).
Build the answer in this order
For static weights, prefix sums plus binary search give O(n) build/O(log n) sample; alias tables give O(n) build/O(1) sample.
For frequent point updates, a Fenwick or segment tree stores subtree weight mass and supports both update and sample in O(log n).
Define behavior for zero/negative weights, total-mass overflow, concurrent updates, and snapshot consistency.
Benchmark based on the real read:update ratio and memory budget, not sampling complexity alone.
A useful interview mental model
This is the shape of a strong answer—not a script to memorize.
Senior-level signal
- Senior answers discuss sharding the mass across machines and selecting shard then item hierarchically.
- Include deterministic replay/versioned weight snapshots when sampling decisions affect experiments or user experience.
What the interviewer is really testing
Likely follow-up questions
Common weak-answer patterns
- Ignoring shape, dtype, device, masking, or broadcasting assumptions.
- Using a framework call without explaining the underlying operation.
- Skipping gradient, numerical-stability, and batching checks.