Design a data structure that supports adding numbers from a stream and finding their median.
Unlock detailed company stats for this questionUpgrade
Design a data structure that supports:
- addNum(num): add an integer from a data stream.
- findMedian(): return the median of all numbers seen so far. If the number of elements is even, return the average of the two middle elements.
Example: addNum(1), addNum(2), findMedian() -> 1.5, addNum(3), findMedian() -> 2.
Possible follow-ups: What if 99% of the numbers are in [0, 100]? Can buckets/counting be used? What if numbers can also be removed? How would you maintain a running 90th percentile? How would you handle the stream if it were distributed across multiple machines?
Format · DSA round.
Community answers
No answers contributed by the community yet.


