The interview problem

Practice prompt

You can retain only 100 records from a stream whose final size is unknown. Design a uniform sample without revisiting earlier records and prove the inclusion probability.

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

Sample stream positions without replacement. Duplicate-valued records remain separate observations. Uniform events are not the same target as uniform users or a sample of the last hour.

2. Worked approach

Fill the first k slots. For arrival t greater than k, draw an integer uniformly from 1 through t. If it is at most k, replace that reservoir slot with the new record; otherwise discard the arrival. Preserve the arrival count and random-generator state in checkpoints.

For t greater than k, the new record enters with probability k/t. An older retained item survives with probability 1−1/t because exactly one of t equally likely draws replaces its particular slot. Induction turns an earlier k/(t−1) inclusion probability into k/t. This establishes equal marginal inclusion; the fixed-size sample has dependence between inclusions.

3. Calculate the example

With k = 2 and four arrivals, record 1 starts retained, survives arrival 3 with probability 2/3 and arrival 4 with probability 3/4. Its final inclusion probability is 1/2. Record 3 enters with probability 2/3 and survives the last arrival with probability 3/4, also 1/2. The sample contains two positions, not four independent coin flips.

4. Failure cases and trade-offs

Resetting t after a restart overweights recent records. Sampling one reservoir per shard and concatenating them overweights small shards. A modulo reduction from a poorly chosen random range can introduce bias. Retrying an event as a new arrival changes the sampled population unless that is the intended semantics.

Reservoir sampling is memory-bounded and simple but does not support deleting expired events to form an exact sliding-window sample. Priority-key sampling can make merges easier, with its own randomness and tie-handling requirements. Weighted sampling requires a different proof.

5. Validate the result

Enumerate tiny streams or run repeated simulations and compare inclusion frequencies and sample size. Test restarts and uneven partitions. Define the population before judging fairness; frequent users naturally occupy more of an event-uniform sample.

6. Interview follow-ups and self-review

How would you sample users instead of events? What state must be restored after failure?

Reward the exact replacement rule, induction, restart behavior and clear population definition.

Sources and related practice

Technical reference: www.cs.umd.edu — Vitter's reservoir-sampling paper; proof walkthrough and examples 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 Estimate expectations with Monte Carlo; Diagnose selection bias in logged recommendation data.

Continue your preparation

Choose a connected learning path or browse more ML fundamentals questions. Read our editorial method and attribution policy.