Skip to question
SystemVerilogDesignVerificationFirmwareArchitectureASIC Interview Questions→
/Interview questions/Estimate stream frequencies with a Count-Min Sketch

Q150·Free·SystemVerilog

Estimate stream frequencies with a Count-Min Sketch

Difficulty
Medium
Topic
Data Structures
Language
SV
Interview prompt

Question

Trade exact counts for bounded memory by implementing a Count-Min Sketch that updates and estimates integer frequencies.

Candidate starting point

Implementation scaffold

class CountMinSketch #(
  parameter int WIDTH = 128,
  parameter int DEPTH = 4
);
  int unsigned counters[DEPTH][WIDTH];

  function new();
    if (WIDTH <= 0 || DEPTH <= 0)
      $fatal(1, "WIDTH and DEPTH must be positive");
    counters = '{default: 0};
  endfunction

  function automatic int unsigned hash_index(int x, int row);
    int unsigned mixed;
    mixed = int'(x) ^ (32'h9e37_79b9 * (row + 1));
    mixed ^= mixed >> 16;
    mixed *= 32'h85eb_ca6b;
    mixed ^= mixed >> 13;
    return mixed % WIDTH;
  endfunction

  function void push(int x);
    int unsigned column;
    // Implement here: update the state for one new observation.
  endfunction

  function int unsigned estimate(int x);
    int unsigned column;
    int unsigned minimum = 32'hffff_ffff;
    // Implement here: return the minimum selected row counter.
  endfunction
endclass
Reviewed 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 3

Taking the minimum across hash rows limits collision inflation; saturating counters prevent wraparound undercounts.

What to cover

Requirements

  1. Maintain one fixed-width counter row for each distinct row-specific hash salt.
  2. push(x) increments one addressed counter in every row.
  3. estimate(x) returns the minimum addressed counter across rows.
  4. Assume each true per-key frequency is at most 32'hffff_ffff; saturate counters so wraparound cannot create an undercount within that bound.
Exact question handoffPractice Q150

Solve it in the question bank, keep your progress, and reveal the reviewed solution when your access allows.

Open in question bank →
Solution accessEach time you open this Solution, one Practice Credit is used; it is not permanently unlocked. Premium Solution content also uses one credit per opening.
Continue learning

Firmware Guide

Review algorithms, data structures, fixed-memory reasoning, concurrency, and silicon bring-up.

  • Data Structures
  • SystemVerilog
  • Count-Min Sketch
  • Hashing
Firmware Guide →
Continue practicing

Related questions

Q214 · Data StructuresMaintain the top-K most frequent stream valuesMediumP→Q145 · Data StructuresTrack top-K frequencies in a sliding windowMedium→Q248 · Probabilistic StructuresCounting Bloom FilterHardP→Q223 · Memory SystemsEstimate the densest page in a bounded streamHardP→
ASIC.FYI · Learn silicon end to end.info@asic.fyi