Find Median from Data Stream - Problem

The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value, and the median is the mean of the two middle values.

For example:

  • For arr = [2,3,4], the median is 3
  • For arr = [2,3], the median is (2 + 3) / 2 = 2.5

Implement the MedianFinder class:

  • MedianFinder() initializes the MedianFinder object
  • void addNum(int num) adds the integer num from the data stream to the data structure
  • double findMedian() returns the median of all elements so far

Answers within 10⁻⁵ of the actual answer will be accepted.

Input & Output

Example 1 — Basic Operations
$ Input: operations = ["MedianFinder","addNum","addNum","findMedian","addNum","findMedian"], values = [null,1,2,null,3,null]
Output: [null,null,null,1.5,null,2.0]
💡 Note: Initialize finder, add 1, add 2, find median (1+2)/2=1.5, add 3, find median 2.0
Example 2 — Single Element
$ Input: operations = ["MedianFinder","addNum","findMedian"], values = [null,5,null]
Output: [null,null,5.0]
💡 Note: With only one element, the median is that element itself
Example 3 — Negative Numbers
$ Input: operations = ["MedianFinder","addNum","addNum","findMedian"], values = [null,-1,-2,null]
Output: [null,null,null,-1.5]
💡 Note: With -1 and -2, the median is (-1 + -2) / 2 = -1.5

Constraints

  • -105 ≤ num ≤ 105
  • There will be at least one element in the data structure before calling findMedian
  • At most 5 × 104 calls will be made to addNum and findMedian

Visualization

Tap to expand
Find Median from Data Stream INPUT Operations Sequence: MedianFinder() addNum(1) addNum(2) findMedian() addNum(3) findMedian() Data Stream: 1 2 3 ... streaming order ALGORITHM STEPS 1 Use Two Heaps MaxHeap (small) + MinHeap (large) MaxHeap smaller MinHeap larger 2 Balance Heaps Keep size diff <= 1 3 addNum Logic Add to maxHeap, then balance 4 findMedian Odd: maxHeap top Even: avg of both tops Time: O(log n) add, O(1) find Space: O(n) FINAL RESULT Execution Trace: addNum(1): max=[1] min=[] median = 1 addNum(2): max=[1] min=[2] median = (1+2)/2 = 1.5 addNum(3): max=[2,1] min=[3] median = 2 Output Array: [null,null,null,1.5, null,2.0] Final Heap State: MaxHeap 2 1 MinHeap 3 OK - All operations complete! Key Insight: Two heaps partition the stream: MaxHeap holds smaller half, MinHeap holds larger half. The median is always accessible at the tops of the heaps in O(1) time, while maintaining O(log n) insertion. TutorialsPoint - Find Median from Data Stream | Optimal Solution (Two Heaps)
Asked in
Google 45 Amazon 38 Facebook 32 Microsoft 28
78.4K Views
High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen