Tutorialspoint
Problem
Solution
Submissions

Find Median from Data Stream

Certification: Advanced Level Accuracy: 100% Submissions: 1 Points: 15

Design a data structure that supports the following two operations:

  1. addNum(int num): Add an integer to the data stream.
  2. findMedian(): Return the median of all elements in the stream.

The median is the middle value in an ordered list of numbers. If the list has an odd number of elements, the median is the middle element. If the list has an even number of elements, the median is the average of the two middle elements.

Example 1
  • Input: ["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"]
  • Input: [[], [1], [2], [], [3], []]
  • Output: [null, null, null, 1.5, null, 2.0]
  • Explanation: MedianFinder object is instantiated. 1 is added to the stream. 2 is added to the stream. findMedian() returns 1.5 as the middle elements are 1 and 2, and their average is 1.5. 3 is added to the stream. findMedian() returns 2.0 as the middle element is 2.
Example 2
  • Input: ["MedianFinder", "addNum", "findMedian", "addNum", "findMedian"]
  • Input: [[], [5], [], [10], []]
  • Output: [null, null, 5.0, null, 7.5]
  • Explanation: MedianFinder object is instantiated. 5 is added to the stream. findMedian() returns 5.0 as there is only one element. 10 is added to the stream. findMedian() returns 7.5 as the middle elements are 5 and 10, and their average is 7.5.
Constraints
  • -10^5 <= num <= 10^5
  • There will be at least one element in the data structure before calling findMedian
  • At most 5 * 10^4 calls will be made to addNum and findMedian
  • The addNum operation should have a time complexity of O(log n)
  • The findMedian operation should have a time complexity of O(1)
HeapPwCApple
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use two heaps: a max heap for the smaller half of the numbers and a min heap for the larger half
  • Balance the heaps so that they have an equal number of elements (or the max heap has one more element for odd counts)
  • The max heap stores the smaller half of numbers, and the min heap stores the larger half
  • The top element of the max heap is the largest of the smaller half, and the top element of the min heap is the smallest of the larger half
  • If the number of elements is odd, the median is the top of the max heap. If even, it's the average of the tops of both heaps

Steps to solve by this approach:

 Step 1: Create two heaps - a max heap to store the smaller half of elements and a min heap to store the larger half.

 Step 2: When adding a number, first add it to the max heap.
 Step 3: Balance the heaps by moving the largest element from the max heap to the min heap.
 Step 4: Ensure the max heap has equal or one more element than the min heap by moving elements back if necessary.
 Step 5: To find the median, if there are an odd number of elements (max heap has one more), return the top of max heap. If even (both heaps are equal in size), return the average of the tops of both heaps.
 Step 6: The time complexity is O(log n) for addNum and O(1) for findMedian, where n is the number of elements.
 Step 7: The space complexity is O(n) to store all the elements across both heaps.

Submitted Code :