Bonus Day 115HardLeetCode

Find Median from Data Stream

Design a structure that accepts numbers one at a time and can report the median of everything added so far at any moment.

Pattern
Two heaps, balanced halves
Time box
40 min
Time
O(log n) per addNum, O(1) per findMedian
Space
O(n)

Try it first — give it 40 minutes

0/4 revealed

Stuck? Take one hint at a time. The struggle is where the pattern sticks.

    The problem in plain English

    Build a class MedianFinder with:

    • MedianFinder(): start with no numbers;
    • addNum(num): add the integer num to the collection;
    • findMedian(): return the median of all numbers added so far, as a decimal.

    The median is the middle value in sorted order. With an even count, it's the average of the two middle values. findMedian is only called after at least one add. Up to 5 × 10⁴ calls in total, and values run from −10⁵ to 10⁵.

    Example:

    addNum(1)      collection [1]
    addNum(2)      collection [1, 2]
    findMedian()   → 1.5
    addNum(3)      collection [1, 2, 3]
    findMedian()   → 2.0

    Start with brute force

    Option A: keep a plain list. addNum appends in O(1). findMedian sorts in O(n log n) and reads the middle. With 5 × 10⁴ calls split evenly, that's about 2.5 × 10⁴ sorts of up to 2.5 × 10⁴ items, roughly 10¹⁰ steps.

    Option B: keep the list sorted. Insert with binary search plus a shift, which is O(n) per add because of the shift. Then findMedian is O(1). With 5 × 10⁴ adds, that's over 6 × 10⁸ element moves in the worst case. Better, but still too slow, and it's not what the interviewer is looking for.

    Building the intuition

    This Hard is two Medium ideas stacked together: "a heap gives you the top of a group in O(1)" (Kth Largest Element in a Stream) and "split the problem at the median" (the same picture as Median of Two Sorted Arrays).

    Step 1: what does the median actually need? Sort the numbers and draw a line down the middle:

     smaller half      |    larger half
     1   3   4   [5]   |   [7]   8   9   12

    The median is (5 + 7) / 2. You never looked at 1, 3, 4, 8, 9 or 12. You only need the largest of the smaller half and the smallest of the larger half. The rest just needs to be on the correct side.

    Step 2: "largest of a changing group" is a max-heap. Keep the smaller half in a max-heap (low), whose top is its largest. Keep the larger half in a min-heap (high), whose top is its smallest. The two tops sit right next to the line.

    Step 3: two rules to maintain.

    • Order: every number in low is ≤ every number in high.
    • Balance: low has the same size as high, or exactly one more.

    With both rules holding: if low is bigger, the count is odd and the median is low's top. Otherwise it's the average of the two tops.

    Step 4: adding without breaking the rules. A new number might belong on either side. A neat trick avoids case analysis:

    1. push num into low;
    2. pop low's top (the largest of low plus num) and push it into high. Now order holds, because whatever moved was ≥ everything left in low;
    3. if high is now bigger than low, pop high's top and push it into low. Balance holds again, and order still holds, because high's smallest is moving down.

    That's at most three heap operations per add.

    The optimal approach

    1. low = max-heap, high = min-heap, both empty.
    2. addNum(num): push into low, move low's top to high, and if len(high) > len(low), move high's top back to low.
    3. findMedian(): if len(low) > len(high), return low's top. Otherwise return (low.top + high.top) / 2.

    The subtle detail: getting a max-heap in each language. Python's heapq is only a min-heap, so low stores negated values: push -num, and read -low[0]. C++'s priority_queue<int> is already a max-heap, and the min-heap is priority_queue<int, vector<int>, greater<int>>. Java uses PriorityQueue with Collections.reverseOrder(). JavaScript has no heap at all, so the solution includes a small MinHeap class that takes a comparison function. Passing (a, b) => a > b turns it into a max-heap.

    The code

    Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.

    import heapq
    
    
    class MedianFinder:
        def __init__(self):
            # low holds the smaller half as a max-heap (heapq is a min-heap, so we
            # store negatives). high holds the larger half as a min-heap.
            # Invariant: len(low) == len(high) or len(low) == len(high) + 1.
            self.low = []
            self.high = []
    
        def addNum(self, num: int) -> None:
            # Push through low first, so the largest of the small half moves up to
            # high. That keeps every element of low <= every element of high.
            heapq.heappush(self.low, -num)
            heapq.heappush(self.high, -heapq.heappop(self.low))
            # Rebalance: low may be bigger by one, never smaller.
            if len(self.high) > len(self.low):
                heapq.heappush(self.low, -heapq.heappop(self.high))
    
        def findMedian(self) -> float:
            if len(self.low) > len(self.high):  # odd count: the middle is low's top
                return float(-self.low[0])
            return (-self.low[0] + self.high[0]) / 2

    Dry run

    Add 5, 2, 8, 1, asking for the median after each.

    AddAfter push into lowAfter moving low's top to highRebalance?lowhighMedian
    5low {5}low {}, high {5}high bigger → move 5 back{5}{}5
    2low {5, 2}low {2}, high {5}sizes equal{2}{5}(2 + 5) / 2 = 3.5
    8low {8, 2}low {2}, high {5, 8}high bigger → move 5{5, 2}{8}5
    1low {5, 2, 1}low {2, 1}, high {5, 8}sizes equal{2, 1}{5, 8}(2 + 5) / 2 = 3.5

    Check the last row: sorted [1, 2, 5, 8], so the median is (2 + 5) / 2 = 3.5. Correct.

    Complexity

    • Time: O(log n) per addNum. At most three heap pushes or pops. O(1) per findMedian: just two peeks.
    • Space: O(n). Every number is stored in exactly one heap.

    Edge cases and traps

    • Integer division. (2 + 5) / 2 must be 3.5. In Java and C++, cast to double before adding. That also avoids int overflow in other versions with large values.
    • Forgetting the order rule. Pushing straight into whichever heap is smaller keeps the sizes balanced but can put a big number in low. Always pass through low and then high, or compare with the tops first.
    • Python sign errors. low holds negatives. Push -num, and read -low[0]. Mixing these up is the most common bug.
    • Balance the other way round. If you let high be the bigger one instead, the odd-count median is high's top. Either convention works. Just pick one and keep findMedian consistent with it.
    • Duplicates. 5, 5, 5 works fine. Ties can sit on either side of the line.

    Say it in the interview

    "The median only depends on the two numbers next to the middle. So I keep the smaller half in a max-heap and the larger half in a min-heap, with the small side allowed to hold one extra. To add, I push into the small heap, move its top to the large heap so the order stays right, and move one back if the large heap gets bigger. The median is either the small heap's top or the average of both tops. That's O(log n) per add, O(1) per query, and O(n) space."

    Follow-ups interviewers ask

    • "All numbers are between 0 and 100." Keep 101 counters. addNum is O(1). findMedian walks the counters to the middle, O(101), which is constant.
    • "99% of numbers are in 0–100." Counters for the common range, plus two small sorted lists (or heaps) for the outliers below and above. The median almost always lands in the counter range.
    • "Median of a sliding window of the last k numbers." (LeetCode 480.) The same two heaps, but you also need to remove the number leaving the window. Use lazy deletion (a hash map of numbers to delete when they reach a top), or use two balanced sorted multisets instead of heaps.

    Practise the same idea

    This is a bonus day of the free DSA plan 🐸

    90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.

    Get the full roadmap — free