Q264FreeFirmware
Exact Streaming Median
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();
endclassReviewed 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
- Support negative values, duplicates, and both odd and even element counts.
- Return failure status 0 from get_median when no values have been observed, leaving its output argument unchanged.
- Target O(log n) push, O(1) median query, and O(n) storage.
- For an even count, average the two middle values without overflowing a 32-bit intermediate.
- Keep the lower and upper partitions balanced after every insertion.

