Approximate frequency counts that never undercount
L215 MINALGORITHMS
PREDICT
You have 256 counters for a skewed stream of 1,782 events over 50 keys. Each key hashes to one counter per row and adds 1 there. In the sim's stream, which layout keeps every estimate within 2% of N (35.6) of the true count?
Per counter, one row of 256 has the smaller average error. The worst key decides, though. With one row of 256, key42 shares a counter with a heavier key and reads 49 for a true 9: over by 40. Four rows of 64 hold: the worst estimate is key25 at 37 for a true 15, because the min ignores the three rows where key25 collided with heavy keys. That outcome depends on where this hash put the heavy keys; it is not a law. Set ONE ROW to WIDTH 256 in the sim and see.
A count-min sketch is d rows of w counters, each row with its own hash. To count a key you add 1 to its counter in every row. To query, you read those d counters and take the minimum.
Each counter holds the key’s true count plus every other key that landed in the same cell. So each row overestimates, and the minimum is the least-polluted row. It never undercounts. With w = e/ε columns, the estimate is at most true + εN with probability 1 − e^(−d), where N is the stream length.
▶ SIMULATION
0 / 7
SKETCH (4 × 64 = 256 counters)
not configured
[x] = the cell key49 hashes to in each row
QUERY key49
cells: — → min 0
true count: 0 · over by 0
ALL 50 KEYS
events N 0 · budget 2%·N 0.0
worst overestimate —
undercounts 0
theory (w = e/ε, d = ln 1/δ)ε = e/64 = 4.2% of N, holds per key with probability ≥ 98%
✓ estimate ≥ true count for every key✓ estimate ≤ true + 2%·N for every key
SCHEDULE
01[SKETCH]configure: 1 row × WIDTH counters (a hashed counter array)
02[STREAM]ingest events 1–446 of 1782
03[STREAM]ingest events 447–892 of 1782
04[STREAM]ingest events 893–1338 of 1782
05[STREAM]ingest events 1339–1782 of 1782
06[QUERY]query all 50 keys and compare with the true counts
07[QUERY]report the worst overestimate against the 2% budget
Start of one row (depth 1) scenario. 7 steps.
COUNT-MIN SKETCH (d rows × w counters, one hash per row)
UPDATE(x, c = 1): for r in 0..d-1: T[r][h_r(x)] += c
QUERY(x): return min over r of T[r][h_r(x)]
Every counter x touches holds x's count plus whatever collided
with it, so every row overestimates and the min never undercounts:
estimate ≥ true count (always)
estimate ≤ true count + ε·N (with probability 1 − δ)
where w = ⌈e/ε⌉, d = ⌈ln(1/δ)⌉, N = total of all updates
Sizing: ε = 0.1%, δ = 1% → w = 2719, d = 5 (13,595 counters)
Mergeable: add two sketches cell by cell (same w, d, hashes).
The bound needs non-negative updates; with deletions the min
can undercount.
Watch the QUERY box and the invariant strip. ONE ROW puts key6 in the same counter as the heaviest key and reports 457 for a true 57. COUNT-MIN uses the same width and the same stream: key25’s four cells are 37, 423, 115 and 41, and the min of 37 is within budget. Neither scenario ever undercounts.
TUNE ITNOT YET
Memory is tight. The 4-row sketch at w = 64 holds the 2% budget with 256 counters. Shrink WIDTH to 52 or less (at most 208 counters) and find a width where every one of the 50 estimates still lands within 2%·N of its true count.
GOAL: every estimate within 2%·N with at most 208 counters (w ≤ 52)
FIX ITNOT YET
Implement update(key, count) and query(key). hash(key, row) returns a column in [0, width). The grader feeds streams with planted collisions and checks that no estimate is below the true count and that a key with one clean row is counted exactly.
IN PRODUCTION
Heavy-hitter detection in network monitoring and DDoS mitigation, top-k queries in stream processors, approximate frequency functions in analytics databases, and per-key rate limiting at scale. The symptom of an undersized sketch is a rare key reported with a heavy key's count.
IN INTERVIEWS
“Find the most frequent search queries over the last hour with fixed memory.” Describe the d × w table, update and min-query, state the one-sided bound estimate ≤ true + εN with w = e/ε, d = ln(1/δ), and pair it with a small heap to track the current top k.
RECALL CARDSTap to flip. They join your review deck when you mark the lesson complete.