Algorithms and Data Structures · Senior · Question 56 of 75

How do you maintain the running median of a stream efficiently?

Explanation

Keep the heaps balanced in size; the median is one of the roots or their average. Each insert is O(log n). A sorted array needs O(n) shifting per insert, and the average is not the median.

Further reading

Problem with a question

Algorithms and Data Structures · Senior · question 56

Test yourself

Take the Algorithms and Data Structures · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz