Coding · must-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix
Find Median from Data Stream
Problem
Design a class MedianFinder that receives integers one at a time and can report the median of everything received so far.
MedianFinder()creates an empty structure.addNum(num)inserts one integer.findMedian()returns the median as a float: the middle value when the count is odd, or the mean of the two middle values when the count is even.
findMedian is only called when at least one number has been added.
- Values fit comfortably in a normal int, positive or negative.
- Up to several tens of thousands of calls to
addNumandfindMediancombined.
Examples
addNum(4),addNum(9),findMedian()→6.5; thenaddNum(2),findMedian()→4.0. Two numbers average to 6.5; with three numbers (2, 4, 9 sorted) the middle one is 4.addNum(7),findMedian()→7.0. A single value is its own median.addNum(-3),addNum(-3),addNum(6),findMedian()→-3.0. Duplicates count separately; the sorted stream is -3, -3, 6.
Hints
- The median splits the data into a lower half and an upper half. Which single value from each half do you need to compute it?
- You need fast access to the largest of the lower half and the smallest of the upper half, plus a cheap way to move one element between halves when they become unbalanced.
- Two heaps: a max-heap for the lower half and a min-heap for the upper half, kept within one element of each other in size.
Last reviewed 8 September 2026
Locked
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Secure payment by Stripe · instant access · refund questions: [email protected]
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.