DescriptionQ229
Q229FWASIC interview problem
Maintain the top-K most frequent stream values
TechniquesSystemVerilogTop KFrequency mapLazy sorting
DifficultyMedium
TopicData Structures
LanguageSystemVerilog
Requirements4 checkpoints
01
Problem
Process a stream of integers and report the K most frequent distinct values seen so far.
Class declarationSystemVerilog
class TopKFrequent;
function new(int k, int sentinel = -1);
function void push(int x);
function int get_kth();
function int[$] snapshot();
endclassExample input and output
Use this case to check your interpretationInput
K=2; stream=[4,4,2,7,2,4]Output
snapshot=[4,2]; frequencies=[3,2]; get_kth()=2Explanation
Exact counts place 4 first and 2 second; value 7 has only one occurrence.
02
Requirements (4)
- push(x) increments the exact frequency of x.
- get_kth() returns the Kth-most-frequent value or the sentinel when fewer than K distinct values exist.
- snapshot() returns up to K values in descending frequency order.
- Equal-frequency tie order may be implementation-defined, but snapshot order must still be nonincreasing by frequency.
