Find Median from Data Stream
Heap / Priority Queue
Asked at 12 companies
11 recent
- IXL
- Adobe
- Spotify
- Intuit
- Amazon
- Apple
- TikTok
- Microsoft
- Bloomberg
+2 moreShow fewer
- DE Shaw
- Cohesity
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 160 pointsSolved (Hard) 0/70Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: Merge sort: split, sort, merge · Partitioning around a pivot
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · Task schedulers and leaderboards: A heap always knows the most urgent job or the top score, so schedulers and “top 10” lists never re-sort everything.
About this problem
Keep the smallest or largest items on top: top-k, streams, and scheduling.
Read the full statement and examples on LeetCode, try it for 20–30 minutes, then come back for the walkthrough and the step-by-step visual.
Examples
Full statement on LeetCode ↗Example 1
- Input
- addNum 5, 15, 1, 3 then findMedian
- Output
- 4
Walk through it step by step(6 steps · shows the approach, so try first)
- Split the numbers into two : LOW (max-heap) holds the smaller half, HIGH (min-heap) the bigger half. Keep them the same size (LOW may have one extra). The is then at their tops.
- Add 5: it passes through LOW so the biggest of the small half moves to HIGH, then rebalance. Median = top of LOW = 5.
- Add 15: it passes through LOW so the biggest of the small half moves to HIGH, then rebalance. Median = (5 + 15) / 2 = 10.
- Add 1: it passes through LOW so the biggest of the small half moves to HIGH, then rebalance. Median = top of LOW = 5.
- Add 3: it passes through LOW so the biggest of the small half moves to HIGH, then rebalance. Median = (3 + 5) / 2 = 4.
- After 5, 15, 1, 3, the median is 4.
Code it on LeetCode
This one needs setup the editor here can't do yet (a class you build, linked nodes, or a helper LeetCode provides), so solve it on LeetCode, with its own tests. Then tick My code passed below, and it joins your reviews. With your LeetCode account linked, that happens on its own.
An error or a Wrong Answer there? What each one means.
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: optimal solution
Solution code
Optimal solutionSplit the numbers into two : a max-heap for the smaller half and a min-heap for the larger half, kept the same size (or the lower one bigger by one). The is the top of one heap or the average of both tops.
class MedianFinder:
def __init__(self):
self.low = [] # max-heap (negated): smaller half
self.high = [] # min-heap: larger half
def addNum(self, num: int) -> None:
heappush(self.low, -num)
heappush(self.high, -heappop(self.low)) # move the largest of low to high
if len(self.high) > len(self.low):
heappush(self.low, -heappop(self.high))
def findMedian(self) -> float:
if len(self.low) > len(self.high):
return float(-self.low[0])
return (-self.low[0] + self.high[0]) / 2Reference solution, tested in Python, Java and C++.
Other ways to solve it
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.