Problem 125 of 198HardLeetCode

Find Median from Data Stream

Heap / Priority Queue

Asked at 12 companies

11 recent

  • IXL
  • Adobe
  • Spotify
  • Intuit
  • Amazon
  • Apple
  • TikTok
  • Microsoft
  • Google
  • Bloomberg
+2 more
  • DE Shaw
  • Cohesity

Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.

  1. 1Basics
  2. 2Paper
  3. 3Code it
  4. 4Watch
  5. 5Visualize
  6. 6Accepted
  7. 7Review

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

Concept video
Learn ›Practise ✎Learn ›Practise ✎Learn ›Practise ✎

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.

  1. 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)
    1. 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.
    2. Add 5: it passes through LOW so the biggest of the small half moves to HIGH, then rebalance. Median = top of LOW = 5.
    3. Add 15: it passes through LOW so the biggest of the small half moves to HIGH, then rebalance. Median = (5 + 15) / 2 = 10.
    4. Add 1: it passes through LOW so the biggest of the small half moves to HIGH, then rebalance. Median = top of LOW = 5.
    5. Add 3: it passes through LOW so the biggest of the small half moves to HIGH, then rebalance. Median = (3 + 5) / 2 = 4.
    6. After 5, 15, 1, 3, the median is 4.

    ▶ Watch these steps animate

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.

Solve on LeetCode

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

Stuck? One hint at a time.

    Srujan's notes aren't here yet. When he solves this one in his journal, his own writeup and solutions show up on this page too.