How to calculate or approximate the median of a list without storing the list
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Calculating the median of a list traditionally involves sorting the entire list and then selecting the middle element, or the average of the two middle elements in the case of an even-sized list. However, this approach requires storing and potentially manipulating the entire dataset, which can be a significant problem when dealing with large data streams or memory-constrained environments. This article explores techniques to approximate or find the median of a list without storing the list, leveraging statistical and algorithmic tricks.
Theoretical Background
The median is a measure of central tendency which represents the middle value of a data set. For a list with an odd number of elements, the median is the middle element. For even numbers, it is the average of the two middle elements. Computing the median from a sorted list has a time complexity of due to sorting, but we aim to improve this with alternative methods.
Approximation Techniques
Reservoir Sampling
Reservoir sampling is a useful technique for selecting a random sample from a large or unknown-sized stream of data. While it is mostly used for random sampling, with appropriate heuristics, it can be adapted to approximate the median:
- Algorithm Explanation: • Begin with an empty reservoir that can hold the required number of samples. • Iterate through the list or stream. • For the first
kelements, fill the reservoir. • For each subsequent element, determine if it should replace an existing element in the reservoir with a probability of , where is the element's index. - Application: • After completion, sort the reservoir and return the middle element as the median approximation. Although sorting is required, it is done only on a fixed-size sample.
The Median of Medians Algorithm
The Median of Medians algorithm is a deterministic selection algorithm for finding the th smallest element in an unordered list in linear time, and it can be adapted to approximate the median:
- Algorithm Steps: • Divide the list into groups of five elements (for largest efficiency). • Sort each group and find the median. • Use a recursive strategy on the array of medians to find the median of these medians. • Use it as a pivot around which the list can be partitioned to make recursive selections easier.
- Approximating Median: • This algorithm is more suited for median finding instead of exact determination, which simplifies computation when exactness is not required.
Probabilistic and Statistical Methods
Streaming Quantiles
Methods such as the P2 Algorithm let us calculate quantiles without storing input data:
- P2 Algorithm: • Maintains five markers for minimum, maximum, and three quantile estimates. • Adjusts markers dynamically as each new data point arrives.
- Benefits: • Provides a dynamic prediction of the median in per new data point with minimal memory usage.
Memory-Efficient Tracking
Using Min-Heaps and Max-Heaps
- Two-Heaps Approach: • Implement two heaps: a max-heap to keep the lower half of the data, and a min-heap for the upper half. • Balance the heaps such that their sizes differ by at most one element. • The median is derived directly from the roots of the heaps.
- Optimal Use Cases: • Efficient for ongoing monitoring of a dynamic data stream with insertions and deletions.
Summary Table
| Technique | Approach | Memory Usage | Complexity | Key Features |
| Reservoir Sampling | Approximation through random sampling | Fixed sample size | & sorting | Randomly approximates the median |
| Median of Medians | Determines th element in | |||
| No large storage required | Ideal for non-exact median determination | |||
| P2 Algorithm | Estimation using marker tracking | Constant space for markers | per update | Minimal space dynamic updates |
| Two-Heaps | Median via heap tracking | when retaining input | per operation | Exact median on streams |
Conclusion
When dealing with large datasets or real-time data streams, the traditional method of sorting an entire list is often impractical due to space or time constraints. By leveraging statistical techniques, heuristic sampling, or intelligent data structures, we can effectively approximate or calculate the median with minimal memory usage. These methods are critical in fields like data analysis, telemetry, and in real-time systems where rapid, efficient processing of data is essential.

