Skip to main content
All Questions

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.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.