Data Structures · Visualization

Count-Min Sketch: Estimate

Count-Min Sketch

Estimation reads the item's d counters and returns the minimum. Time: O(d) reads. Space: O(1) extra.

Big-O: Add/estimate in O(d) hashes; tiny fixed memory.

Count-Min Sketch: Estimate

Count-Min Sketch

Estimation reads the item's d counters and returns the minimum. Why the min? Every collision adds phantom counts, so each counter is truth-plus-noise and the smallest has the least noise. The answer is never below the truth — one-sided error you can budget for with width and depth.

How Estimate Works

  1. Hash the item with all d functions to find its cells.
  2. Read the d counter values.
  3. Return the minimum — the least-inflated witness.
  4. Error bound: with width w and depth d, overestimate stays small with high probability.

Estimate Worked Example

Input: Estimate 'apple' and 'fig' (never added)

  1. 'apple' cells read (3, 2) → min = 2 (truth 2).
  2. 'fig' hashes to (row0:1, row1:0) → reads (3, 1) → min = 1.
  3. 'fig' never occurred yet estimates 1 — a collision overestimate.
  4. Wider grid (more columns) makes such collisions rarer.

Output: apple ≈ 2 ✓ · fig ≈ 1 (phantom — never underestimates)

Try your own input
Count-Min: Introduction
A tiny counter grid estimating frequencies — never underestimates.
row 0
0
0
0
0
0
0
0
0
row 1
0
0
0
0
0
0
0
0
row 2
0
0
0
0
0
0
0
0
Touched cell Non-zero counter
Count-Min: Introduction
Press ▶ Run to watch it step by step.
Python · Count-Min Sketch in Code
def estimate(sketch, x):
    return min(sketch[r][h(r, x)] for r in range(D))
    # min = least-inflated witness; never underestimates
# example: estimate
Input: Estimate 'apple' and 'fig' (never added)

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Estimate Time & Space Complexity

TimeO(d) reads
SpaceO(1) extra

When to Use Estimate

  • Top-K approximation: estimate then keep a small exact leaderboard
  • Database join-size estimation for query planners

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.