Incremental median computation with max memory efficiency
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Incremental median computation is a highly efficient method for processing data streams or large datasets when you want to compute the median in real-time with minimal memory usage. In this article, we will delve into the detailed methodology of computing medians incrementally, explore the data structures needed, and provide practical examples that illustrate the method's effectiveness.
Introduction to Incremental Median Computation
In data analysis, the median is a measure of central tendency that is particularly useful when dealing with skewed distributions. Calculating the median efficiently is crucial for applications where data arrives continuously, such as in finance and sensor data processing.
Why Incremental?
Incremental computation of the median is advantageous because:
- It provides real-time results.
- It reduces memory usage significantly compared to storing all data points.
- It avoids recomputation as new data arrives.
Technical Explanation
Data Structures
To achieve incremental median computation, we need a strategy that enables efficient insertion, deletion, and median-finding. Two heaps (priority queues) are commonly used for this purpose:
- Min-Heap: Contains the larger half of the data.
- Max-Heap: Stores the smaller half of the data.
Properties:
- The top of the Max-Heap (max element) is less than or equal to the top of the Min-Heap (min element).
- Balancing the heaps ensures that the medians can be directly accessed from the top elements.
Algorithm
When a new element arrives:
- Insertion:
- If the new element is less than or equal to the top of the Max-Heap, insert it into the Max-Heap.
- Otherwise, insert it into the Min-Heap.
- Balance:
- If the size of the Max-Heap exceeds the size of the Min-Heap by more than one, move the top element from the Max-Heap to the Min-Heap.
- Conversely, if the Min-Heap has more elements, move the top element to the Max-Heap.
- Compute Median:
- If both heaps have the same size, the median is the average of the tops of the Max-Heap and Min-Heap.
- If one heap has more elements, the median is the top of that heap.
Example
Consider these steps with a series of numbers: 10, 20, 30, 40, 50.
Steps:
- Insert 10 into Max-Heap.
- Insert 20 into Min-Heap.
- Balance: (No balancing needed here)
- Median is (Max-Heap top + Min-Heap top)/2 = (10 + 20)/2 = 15.
Now assume the next elements (30, 40, 50):
- Continue inserting into appropriate heaps, balancing, and computing the median using the aforementioned logic.
Memory Efficiency
The memory usage in this method is optimal for streaming data scenarios:
- Instead of storing all data points, only the elements needed to maintain two balanced heaps are stored.
- The time complexity for insertion, balancing, and finding the median is .
Comparison with Other Methods
| Method | Memory Usage | Time Complexity | Suitable for Streaming |
| Incremental Median (Heaps) | total, with constant space per element | for insertion and balancing, for median finding | Yes |
| Sort and Find Median | No | ||
| Reservoir Sampling | (where ) for limited sample | time to compute median | No |
Conclusion
The incremental median computation provides a robust, memory-efficient solution for real-time data analysis. By leveraging binary heaps, it facilitates constant-time median finding and logarithmic-time insertion, making it an ideal choice for applications in finance, IoT, and any real-time system. Understanding and implementing this technique can substantially optimize performance and resource usage for data-intensive systems.
Related reading
- Incremental Nearest Neighbor Algorithm in Python
- Incremental training of ALS model
- incremental way of counting quantiles for large set of data
- Infer multivalent features with tfdv from pandas dataframe
- Incremental price graph approximation
- Index Of Longest Run C
- Indexing ranked permutations into other ranked permutations
- Initializing Half-edge data structure from vertices

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.