Skip to the selected question
ASIC.FYI

ASIC Interview Question Bank

1,000+ hardware interview questions.Curated and reviewed by industry engineers.1,000+ hardware questions1,000+ questionsEngineer-reviewed
DescriptionQ209
Page ↗
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();
endclass

Example input and output

Use this case to check your interpretation
Input
K=3; stream=[4,9,1,9,7]
Output
snapshot=[9,9,7]; get_kth()=7
Explanation

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.