Find Median from a Data Stream
Try to solve the Find Median from a Data Stream problem.
We'll cover the following
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,
Implement a class, MedianOfStream, which should support the following operations:
Constructor(): This initializes the object of this class, which in turn creates the max and the min heap.
Insert Num(num): This adds an integer,
num, to the data structure.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:
num, where numis 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,
calls will be made to the function that calculates the median.
Examples#
1 of 2
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
What should be the output if the following number stream is given as input?
[12, 14, 36, 54]
25.0
29.0
36.0
14.0
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.
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.
Solution: Maximize Capital
Solution: Find Median from a Data Stream