← All problems

Find Median from Data Stream

Hard heapsdesign LeetCode ↗
Design a data structure supporting addNum (insert a number from a stream) and findMedian (return the median of all numbers seen so far). Both operations should be efficient for a long stream.

Hints (0/3 revealed)

Log in to reveal hints progressively.

Complexity

Time O(log n) add, O(1) median; Space O(n)

Common pitfalls

  • Broken size invariant after inserts
  • Python heapq is min-heap: negate for max-heap

Your solution

Log in to save your code and track progress.