Skip to the workspace
Pebellslearn by building
Probabilistic & System-Scale Structures  ›  Lesson

Sampling a Stream You Can't Store

Reservoir Sampling 24 minKeep k items fairly from a stream of unknown length: each later item gets a k/(i+1) chance to swap in

dsa / Sampling a Stream You Can't Store

Explore each scene at your own pace.

01 · Audit a fair sample of a day's requests

Audit a fair sample of a day's requests

Sampling a Stream You Can't Store

Definition
Reservoir sampling picks k items uniformly at random from a stream of unknown length in a single pass using only k slots of memory. Each new item replaces a random held item with a probability that shrinks as the stream grows.
In plain words
Imagine keeping a handful of raffle tickets from a conveyor belt that never says when it will end. You keep the first few, then each new ticket gets a chance to swap in, a smaller chance each time, so every ticket ends up equally likely to stay.

In short: to pick k items uniformly at random from a stream whose total length you don't know in advance, keep the first k items, then for every later item i (0-indexed), draw a random j between 0 and i. If j < k, that item swaps into slot j of the reservoir; otherwise it's dropped. Item i ends up with exactly a k / (i + 1) chance of surviving, and working through the algebra shows every item in the whole stream — no matter when it arrived — ends up with the same k / n chance of being in the final reservoir. The reservoir never grows past k items, however long the stream runs.

A k-cell reservoir, and one item fighting to get in

Audit a fair sample of a day's requests

Quarry can't store a full day of request logs just to pick a few for a spot-check, but it can prove the sample it does keep is fair by re-running the draw many times and counting how often each request survives:

python

With 5 requests and a reservoir of 2, every request should appear in roughly 1000 * 2 / 5 = 400 of the 1000 trials. Every seeded trial is reproducible — the same seed and trial index always draw the same j values, which is what makes sample_counts itself deterministic even though each individual draw is random.

Watch one stream of 5 items fill a 2-cell reservoir, replace once, get rejected once, then replace again:

watch it move · array

Each later item rolls a random j in [0, i]. j < k swaps it in; j >= k rejects it. The reservoir never grows past k cells.

Item 0, "p", fills the first empty slot — nothing to roll yet. 2 cells. At index 0.

1 / 5
Scene 1 of 7
Project context and further resources
You're building a piece ofQuarry — the storage engine
This piece — sample_counts(): Spot-checks a fair sample of a day's requests without storing the whole day.
Scenario Quarry needs to spot-check a fair sample of a day's requests without storing the full day, and has to prove the sample is actually fair, not just plausible.
sample_counts.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
sample_counts(stream, k, seed, trials) → dictHow many of `trials` reservoir-sample runs kept each stream item, proving the k/n fairness guarantee.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.