Skip to question
ASIC.FYI
DesignVerificationSystemVerilogFirmwareArchitectureInterviews
ASIC.FYI/Interview questions/Exact Streaming Median

Q264·Free·Firmware

Exact Streaming Median

Difficulty
Hard
Topic
Data Structures
Language
SV
Interview prompt

Question

Continuously process signed integers and report the exact median of all values seen so far. The implementation must not sort the complete history on every query.

Starting point

Question code

class StreamingMedian;
  void push(int x);
  bit get_median(output real median);
  int size();
endclass
Reviewed example

Trace one case

Input
stream = [5, 1, 9, 3]
Expected output
median after each insertion = [5, 3, 5, 4]

For even counts the exact median is the average of the two middle values; 1 and 5 therefore yield 3, then 3 and 5 yield 4.

What to cover

Requirements

  1. Support negative values, duplicates, and both odd and even element counts.
  2. Return failure status 0 from get_median when no values have been observed, leaving its output argument unchanged.
  3. Target O(log n) push, O(1) median query, and O(n) storage.
  4. For an even count, average the two middle values without overflowing a 32-bit intermediate.
  5. Keep the lower and upper partitions balanced after every insertion.
Exact question handoffPractice Q264

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

Open in question bank →
Solution accessA Free account unlocks the complete reviewed solution.
Continue learning

Firmware

  • Data Structures
  • Two heaps
  • Streaming
  • Numerical safety
Firmware interview questions →
Continue practicing

Related questions

Q217 · Data StructuresExpiring Key-Value Store→Q266 · Data StructuresLongest-Prefix-Match Routing Table→Q273 · Data StructuresReturn the first unique value in a stream→
ASIC.FYI · Learn silicon end to end.info@asic.fyi