Q168FreeSystemVerilog
Estimate stream frequencies with a Count-Min Sketch
Interview prompt
Question
Trade exact counts for bounded memory by implementing a Count-Min Sketch that updates and estimates integer frequencies.
Starting point
Question code
class CountMinSketch #(
parameter int WIDTH = 128,
parameter int DEPTH = 4
);
function void push(int x);
function int unsigned estimate(int x);
endclassReviewed example
Trace one case
Input
push(A) three times; push(B) once; estimate(A)Expected output
estimate(A) >= 3 and never below the true count 3Taking the minimum across hash rows limits collision inflation; saturating counters prevent wraparound undercounts.
What to cover
Requirements
- Maintain one fixed-width counter row for each distinct row-specific hash salt.
- push(x) increments one addressed counter in every row.
- estimate(x) returns the minimum addressed counter across rows.
- Assume each true per-key frequency is at most 32'hffff_ffff; saturate counters so wraparound cannot create an undercount within that bound.

