Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Reservoir Sampling proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Maintain an unbiased sample from a stream whose total length is unknown in advance. Learn the algorithm's preconditions, state transition, correctness argument, Python cost model, and transfer from one focused drill to a full Interview Problem.
Recognize it when
Consider Reservoir Sampling when the prompt's constraints and required operations match this shape: Maintain an unbiased sample from a stream whose total length is unknown in advance.

Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Reservoir sampling selects from a stream of unknown length without storing all items. After processing i items, the reservoir invariant gives each seen item equal inclusion probability.
For a one-item reservoir, keep the first item, then replace it with item i using probability one over i when counting items from one. An integer draw over all seen positions implements that probability exactly.
def reservoir_trace(values,draws):
if not values:return []
sample=values[0];trace=[]
for index,item in enumerate(values[1:],1):
draw=draws[index-1];replaced=draw==0
if replaced:sample=item
trace.append([item,draw,replaced,sample])
return traceImplement reservoir_trace(values,draws). For item index i after the first, draws supplies an integer in [0,i]; return [item,draw,replaced,sample].
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Fill k reservoir slots, then for seen count i draw uniformly from zero through i minus one. Replace a slot only when the draw is below k, preserving inclusion probability k over i.
def sample_one(values,draws):
if not values:return None
sample=values[0]
for index,item in enumerate(values[1:],1):
if draws[index-1]==0:sample=item
return sampleImplement sample_one(values,draws). Return one reservoir sample using supplied draws for testability, or None for empty input.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Production code may accept an RNG object; tests should inject deterministic draws. Modulo-reducing arbitrary random integers can introduce bias unless the source range divides evenly.
Say: “After i items, each has probability one over i of occupying the sample. The new item replaces with that probability, while every prior item survives with the complementary probability.” State one-pass time, O(k) space, and empty behavior.
Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Reservoir Sampling proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| Reservoir Sampling complete workflow | O(n) | O(n) | Seed k items, then map each supplied uniform stream-index draw to either one replacement or no change. |
O(k) for the focused Simulate Reservoir Sampling implementation.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
Verify algorithm preconditions such as sorted input, nonnegative weights, acyclicity, or admissible heuristics before applying it.
Prevent it: State and verify this precondition before coding: Each random choice is uniform over the documented candidate range and uses an unbiased random source.
Avoid
def reservoir_sample(values, k, draws):
passUse instead
def reservoir_sample(values, k, draws):
reservoir = list(values[:k])
for stream_index, value in enumerate(values[k:], start=k):
draw = draws[stream_index - k]
if draw < k:
reservoir[draw] = value
return reservoirWhere you will hit this: Simulate Reservoir Sampling(opens in a new tab)
Appends selected values and lets the reservoir grow.
Prevent it: Preserve this proof obligation: Induction shows each eligible item or permutation retains the required equal probability after every update.
Avoid
def reservoir_sample(values,k,draws):
out=list(values[:k])
for value,draw in zip(values[k:],draws):
if draw<k:out.append(value)
return outUse instead
def reservoir_sample(values, k, draws):
reservoir = list(values[:k])
for stream_index, value in enumerate(values[k:], start=k):
draw = draws[stream_index - k]
if draw < k:
reservoir[draw] = value
return reservoirWhere you will hit this: Simulate Reservoir Sampling(opens in a new tab)
Include visited state, boundary cases, recursion depth, and hidden copying costs in correctness and complexity analysis.
Prevent it: Count every sort, slice, copy, membership check, heap update, and recursive frame before claiming O(n).
Avoid
def reservoir_sample(values,k,draws):
out=list(values[:k])
for value,draw in zip(values[k:],draws):
if draw<k:out.append(value)
return outUse instead
def reservoir_sample(values, k, draws):
reservoir = list(values[:k])
for stream_index, value in enumerate(values[k:], start=k):
draw = draws[stream_index - k]
if draw < k:
reservoir[draw] = value
return reservoirWhere you will hit this: Shortest Subarray With OR at Least K(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27