Find Median from Data Stream
Find Median from Data Stream is a Hard Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Heap (Priority Queue) Patterns pattern, in the Two Heaps for Median Finding subpattern.
Problem statement
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 example, 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-5 of the actual answer will be accepted.
Example 1:
Input ["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"] [[], [1], [2], [], [3], []] Output [null, null, null, 1.5, null, 2.0]
Explanation MedianFinder medianFinder = new MedianFinder(); medianFinder.addNum(1); // arr = [1] medianFinder.addNum(2); // arr = [1, 2] medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2) medianFinder.addNum(3); // arr[1, 2, 3] medianFinder.findMedian(); // return 2.0
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.
Follow up:
- If all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?
- If 99% of all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?
Topics and companies
Topics: Two Pointers, Design, Sorting, Heap (Priority Queue), Data Stream.
Reported in interviews at Amazon, Facebook, Microsoft, ByteDance, Google, Apple, eBay, Bloomberg, Salesforce, Goldman Sachs, Nvidia, Uber and others.
How to practise Find Median from Data Stream on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(log n) per addNum, O(1) per findMedian. Each insertion into a heap is O(log n), and balancing is also O(log n). Finding the median is O(1) since it just checks the tops of the heaps. time and O(n), where n is the number of elements added. Both heaps together store all elements. space.
Open Find Median from Data Stream in the code editor, or read the Find Median from Data Stream editorial for a worked solution with its approach and complexity analysis.
Where Find Median from Data Stream sits in the DSA pattern sheet
- Heap (Priority Queue) Patterns pattern guide
- Two Heaps for Median Finding subpattern
- The full DSA patterns sheet
- All coding practice problems
Problems related to Find Median from Data Stream
Other problems that use the same Two Heaps for Median Finding and Heap (Priority Queue) Patterns techniques:
- Finding MK Average — Hard, same subpattern
- Find K Pairs with Smallest Sums — Medium, same pattern
- Furthest Building You Can Reach — Medium, same pattern
- K Closest Points to Origin — Medium, same pattern
- Kth Largest Element in an Array — Medium, same pattern
- Kth Largest Element in a Stream — Easy, same pattern