AI engineering glossary · Data structures algorithms
What is reservoir sampling?
Reservoir sampling: A randomized method for picking k items uniformly from a stream whose length is unknown, keeping only k items in memory. The i-th item replaces a stored one with probability k/i.
Reservoir sampling: an example
To pick one random tweet from a live stream, keep the first, then replace it with the 2nd with probability 1/2, the 3rd with 1/3, and so on.
Related terms
More Data structures algorithms terms
- Segment tree
- Selection sort
- Separate chaining
- Shortest path
- Sliding window
- Sorting
- Space complexity
- Space-time trade-off
Where you learn it
Reservoir sampling is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.