workshop private

← all creations

Leading Zeros

viz · created 2026-09-16

Cardinality estimation as a gallery of coin flips — every item hashed, its run of leading zeros lit up, and one register shouting an answer that can only ever be a power of two. Split the stream across a thousand registers and the estimate walks in toward the truth; reroll the hash and watch the promised error band get drawn by the runs themselves. Then merge two sketches by taking the per-register max and count a union neither side ever saw.

algorithmsinterview-prepcanvas

HyperLogLog, one arrival at a time. Every item is hashed to 32 bits; the top p bits pick a register, and the run of leading zeros in what’s left is the evidence, because a run of k zeros turns up about once every 2^k hashes. Each register keeps the longest run it has ever seen — a max, which is the whole reason this works on a stream you cannot afford to deduplicate.

Three things are worth watching, in this order.

One register shouts. Start at 1 register and the estimate is 2^M, the longest run seen. It is not merely inaccurate, it is quantised: 8,192 or 16,384 or 65,536 and never anything in between, because powers of two are the only answers it has. One unusually long run early in the stream pins it there for the next twenty thousand items, and the error curve decays hyperbolically toward zero and then jumps the moment M ticks up — a sawtooth that is the shape of an estimator with a sample size of one.

A thousand registers whisper. Switch to m registers and the same evidence, split across 1024 slots and averaged with a harmonic mean, walks in toward the truth and stays there. The header states the promise — 1.04/√1024 = ±3.25% — and the curve settles inside it.

Then the promise gets checked. A single run landing inside the band proves nothing about the next one, so the error band is not something this piece asks you to take on faith. reroll hash ⟳ replays the same stream with a fresh hash and keeps the old curve; +10 runs does it ten times at once. The band fills in with curves until the envelope is visibly the one the formula named. That is the difference between a standard error and a number in a header: the error is a property of the hash, not of the data, and it is a distribution rather than a value.

The merge is the reason this is in your database. merge two sketches runs two streams that never see each other’s items, sketches each one, and takes the per-register max. Three answers to “how many distinct across both?” sit side by side: the exact union, the merged sketch (within a few percent), and est(A) + est(B), which at 100% overlap is 110% too high because it counts every shared key twice. Two sketches, 1.5 KB between them, and the union comes out right. No exact set gives you that at any price — you would have to ship the sets themselves.

Two details the viz shows rather than asserts:

Reuse

src/leading-zeros.js is a framework-free ES module:

No rendering or timers in the module; the canvas demo is reference code.

Gotchas