Maintain streaming median and loosemedian
GoogleSoftware Engineer · OnsiteLast updated
This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Maintain streaming median and loosemedian states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Design a data structure for an online stream of positive integers supporting insert (x). After each insertion, output: (a) the median of all values seen so far (for an even count, define the median as the average of the two middle values); and (b) the 'loose median' interval [2^k, 2^(k+ 1)] where k is the unique integer satisfying 2^k < median < 2^(k+ 1). Describe the algorithms and data structures, their time and space complexities, and how to implement bit operations to compute 2^k from the median. Discuss how you would handle boundary cases (e.g., median equals a power of two, zero/negative numbers, or non-integer medians).
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.