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

Count-Min & HyperLogLog

Sketches 26 minCount occurrences in a d x w grid and take the min to query; estimate distinct counts from the longest run of leading zeros seen

dsa / Count-Min & HyperLogLog

Explore each scene at your own pace.

01 · Count events by key without a dict per key

Count events by key without a dict per key

Count-Min & HyperLogLog

Definition
A sketch is a small fixed-size summary of a large stream that gives approximate answers. A count-min sketch estimates how often each key occurred using a grid of counters and hashes; HyperLogLog estimates how many distinct keys there were.
In plain words
Rather than keeping a notebook with a line for every visitor, you keep a few tally boards that several visitors share. The answers can be slightly high or low, but the boards stay tiny however many visitors arrive.

In short: a count-min sketch counts occurrences of many keys in a fixed d × w grid instead of one counter per key. Every event increments one cell per row — a different column in each row, picked by a different hash function. A query reads all d of a key's cells and reports the minimum, because a cell can only be inflated by collisions with other keys, never deflated: the smallest cell is always the closest to the truth, and the estimate is never below the real count. HyperLogLog answers a different question — not "how many of X" but "how many distinct keys overall" — using the same "give up exactness, buy back memory" trade: it tracks the longest run of leading zero bits seen per bucket, because seeing a long run of zeros is rare enough that it implies a lot of distinct hashes were tried.

A 2x4 counter grid from the real events cat,cat,dog,fox: "cat" reads correctly at 2 because row 0's cell is never shared, "dog" reads correctly at 1, but "fox" shares a cell with dog in row 0 and with cat in row 1, so it reads 2 instead of its true count of 1 — a collision can only push the estimate up, never down

Count events by key without a dict per key

Quarry's live dashboard tracks how often each of millions of possible event types fires, in bounded memory:

python

With only 4 columns per row, "dog" and "fox" collide in row 0, and "cat" and "fox" collide in row 1 — so "fox"'s two cells are 2 and 3, and its estimate comes out as min(2, 3) = 2, one more than its true count of 1. "dog" still reads correctly at 1, because row 1's cell for "dog" wasn't shared with anything. More rows means more independent chances to dodge a collision — that's the entire reason count-min uses more than one hash function.

Note the hash: Python's own hash() changes between runs unless PYTHONHASHSEED is fixed (see the Bloom filters lesson), so this uses hashlib.md5 keyed on a seed string instead — fully reproducible, on any machine, forever.

Watch the grid fill as 4 events arrive, then see the query take the min:

watch it move · grid

Each event lights one cell per row. Querying a key takes the MIN across its d cells — collisions can only push an estimate up, never down.

Event 1, "cat", lights row 0 col 2 and row 1 col 2. 2 by 4 table. 8 of 8 cells filled.

1 / 7
Scene 1 of 6
Project context and further resources
You're building a piece ofQuarry — the storage engine
This piece — count_min_estimate(): Counts how often each of millions of possible event types fires, in bounded memory.
Scenario Quarry's live dashboard tracks how often each of millions of possible event types fires, in a fixed, bounded amount of memory.
count_min_estimate.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
count_min_estimate(events, queries, d, w, seed) → dictEstimated occurrence counts for each queried key, from a fixed-size d x w counter grid.
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.