Find Median from a Data Stream

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


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 struct, MedianOfStream, which should provide the following functionalities:

  1. Init(): Initializes the data structure, setting up the necessary components to manage the stream of integers, i.e., it creates the max and the min heap.

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

  3. Find Median(): 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.


  • 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.

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy