DescriptionQ209
Q209FWASIC interview problem
Maintain the K largest stream values
TechniquesSystemVerilogTop KSorted queueStreaming
DifficultyMedium
TopicData Structures
LanguageSystemVerilog
Requirements4 checkpoints
01
Problem
Continuously process integers and retain the K largest values seen so far, including duplicate occurrences.
Class declarationSystemVerilog
class TopKLargest;
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=3; stream=[4,9,1,9,7]Output
snapshot=[9,9,7]; get_kth()=7Explanation
Duplicate observations are retained, and the bounded structure discards 4 and 1 as smaller than the final top three.
02
Requirements (4)
- Keep at most K real stream values rather than pre-filling the structure with sentinels.
- get_kth() returns the Kth largest value or the sentinel before K values have arrived.
- snapshot() returns retained values in descending order.
- Reject nonpositive K.
