CAPSTONE: TOP-K IN A STREAM
Everything in this path, in one trending-topics service
A trending service estimates each topic's count with a count-min sketch, which never underestimates, and keeps the 5 highest estimates in a small heap. Can it still drop a true top-5 topic from the report?
You need two answers from the stream: the top K topics and how many distinct topics there are. Exact counting stores every key, so memory grows with the stream.
Three fixed-size structures replace it. A count-min sketch estimates any topic’s count; it can only overestimate. A min-heap of K entries holds the current leaders: after each event, the topic enters if its estimate beats the heap’s minimum. A HyperLogLog estimates distinct topics. Like a Bloom filter, each one hashes into a fixed array and accepts a known error.
const counts = new Map();
for (const tag of stream) counts.set(tag, (counts.get(tag) || 0) + 1);
// memory = one entry per distinct tag, forever
const top = [...counts].sort((a, b) => b[1] - a[1]).slice(0, K);for (const tag of stream) {
cms.add(tag); // d × w counters, fixed
hll.add(tag); // m registers, fixed
const est = cms.estimate(tag); // never under the true count
const e = heap.find(x => x.tag === tag);
if (e) e.est = est; // already a leader: refresh
else if (heap.length < K) heap.push({ tag, est });
else if (est > heap.min().est) heap.replaceMin({ tag, est });
} Watch SERVICE MEMORY and the invariant strip. EXACT HASH MAP reports the right answer but needs 4,800 cells for 2,400 distinct topics, almost five times the budget. SKETCH + HEAP + HLL uses 586 cells from the first event to the last and reports the same five leaders. Mid-stream it briefly ranks #strike above #eclipse, which are 4 events apart. The distinct count is within about 12%.
The sketch stays inside the budget at any width up to 232. Shrink SKETCH WIDTH until collisions push a true top-5 topic out of the report.
GOAL: a true top-5 topic is missing from the report
This topK keeps at most k candidates but never checks whether the arriving item is already one of them, so a hot topic takes several slots. Keep at most one entry per item: refresh its estimate if it is already in the heap, otherwise add it or replace the minimum. Return the k items, highest estimate first. Use only sketch.add(item) and sketch.estimate(item) for counts. stream is a one-pass iterator, and memory is the point: once it ends you may look up estimates for at most k items.
Trending and heavy-hitter detection (hashtags, hot keys in a cache, top talkers in network flow monitoring), Redis's RedisBloom module (Count-Min Sketch and Top-K), Redis PFCOUNT for HyperLogLog, and stream processors that report approximate top-N per window.
“Design trending hashtags” or “find the top K items in a stream that does not fit in memory.” Start with the exact answer and its memory cost, then count-min plus a size-K heap, say which way the error goes and how width and depth bound it, add HyperLogLog for distinct counts, and mention windowing (one sketch per time bucket) for “trending now”.