Find Median from a Data Stream

Try to solve the Find Median from a Data Stream problem.

Statement #

Create a data structure that can store a list of integers that can change in size over time and find the median from this dynamically growing list in constant time, O(1)O(1).

Implement a class, MedianOfStream, which should support the following operations:

  1. Constructor(): This initializes the object of this class, which in turn creates the max and the min heap.

  2. Insert Num(num): This adds an integer, num, to the data structure.

  3. Find Median(): This finds the median of all elements seen so far. If there are an even number of elements, return the average of the two middle values.

Constraints:

  • 105-10^5 \leq num 105\leq 10^5, where num is an integer received from the data stream.

  • There will be at least one element in the data structure before the median is computed.

  • At most, 500500 calls will be made to the function that calculates the median.

Examples#

Created with Fabric.js 3.6.6 Sample example 1Input Output median = 30.0 Stream 22 35 30

1 of 2

Created with Fabric.js 3.6.6 median = 27.5 Sample example 2Input Stream 22 35 30 25 Output

2 of 2

Understand the problem#

Take a moment to make sure you’ve correctly understood the problem. The quiz below will help you check if you’re solving the correct problem:

Find Median from a Data Stream

1

What should be the output if the following number stream is given as input?

[12, 14, 36, 54]

A)

25.0

B)

29.0

C)

36.0

D)

14.0

Question 1 of 30 attempted

Figure it out!#

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Drag and drop the cards to rearrange them in the correct sequence.

Split up incoming numbers into two lists—small and large. Those that are smaller than the current middle element are small, and those that are larger than it are large.

Maintain these lists as heaps so that the root of the small heap is the largest element in it and the root of large heap is the smallest element in it.

If the size of the large heap is greater than the size of small heap or, if size of small heap is greater than the size of the large heap + 1, rebalance the heaps.

If the number of elements is even, the median is the mean of the root of the two heaps. Else, it’s the root of the small heap.


Try it yourself#

Implement your solution in main.py in the following coding playground. We have provided some useful code templates in the other files that you may build on to solve this problem.

Python
median_of_stream.py
min_heap.py
max_heap.py
Input #1
Input #2
Find Median from a Data Stream

Solution: Maximize Capital

Solution: Find Median from a Data Stream