Article start
DSA Course: Interview Patterns and Problem Solving
Module 12: Heap & Priority Queue

Median Finder: Two Heaps Pattern

Maintain a running median from a stream of numbers.

May 29, 2026·25

Learning Outcome

After this lesson, you should be able to split a stream into lower and upper halves with two balanced heaps.

Problem Statement

Design a data structure that supports addNum and findMedian for a stream of integers.

InputOutputWhy
add 1, add 2, findMedian, add 3, findMedian1.5, then 2After [1,2], median is average 1.5. After [1,2,3], median is 2.

Brute Force Approach

Store all numbers and sort after every insertion or median query. This makes repeated operations slow.

Optimized Approach

Use a max-heap for the lower half and a min-heap for the upper half. Rebalance so sizes differ by at most one.

Exact Pseudocode

addNum(x):
  add x to lower half
  move largest lower value to upper half
  if upper half is larger:
    move smallest upper value to lower half

findMedian():
  if lower has more values:
    return lower top
  return average of both tops

Reference Code

import heapq

class MedianFinder:
    def __init__(self):
        self.left = []
        self.right = []

    def addNum(self, num):
        heapq.heappush(self.left, -num)
        heapq.heappush(self.right, -heapq.heappop(self.left))
        if len(self.right) > len(self.left):
            heapq.heappush(self.left, -heapq.heappop(self.right))

    def findMedian(self):
        if len(self.left) > len(self.right):
            return -self.left[0]
        return (-self.left[0] + self.right[0]) / 2

Sample Dry Run

StepStateResult
Add 1left=[1], right=[]median is 1
Add 2left=[1], right=[2]median is 1.5
Add 3left=[2,1], right=[3]median is 2
Invariantleft top <= right toptops define median

Complexity

MeasureValueReason
TimeO(log n) add, O(1) medianEach add performs heap push and pop operations.
SpaceO(n)All stream values are stored across the two heaps.

Edge Cases

  • Even count returns average of both heap tops.
  • Odd count returns the top of the larger heap.
  • Heap sizes should differ by at most one.

Interview Checklist

  • Keep lower half in a max-heap.
  • Keep upper half in a min-heap.
  • Rebalance after every insertion.

FAQs

Why two heaps?

They keep quick access to the largest lower-half value and smallest upper-half value.

Why rebalance?

The median depends on the middle one or two values, so heap sizes must stay close.

What is the core pattern?

Two heaps for streaming median.

Test your knowledge

Take a quick quiz based on this chapter.

Discussion

0 comments

Sign in to share a question or add to the discussion.
Start the discussion

Ask a question or share what stood out to you.