The Design an Array Statistics Tracker interview question involves building a system that processes a stream of numbers and provides real-time statistics. You need to support adding elements and querying the mean, median, and mode. This Design an Array Statistics Tracker coding problem is particularly challenging because it requires maintaining three different properties that respond differently to new data.
Amazon asks this to test your ability to balance the computational cost of different operations. It evaluates your knowledge of Heap (Priority Queue) interview patterns for medians and Hash Table interview patterns for modes. It's a test of complex state synchronization.
runningSum and a count. Mean is sum / count ().Map<Integer, Integer> for frequencies and a secondary structure (like an Ordered Map or another Map of Frequency -> Set of Numbers) to track which number has the highest count ( or update).Stream: [1, 2, 2, 3].
sum=8, count=4. Mean = 2.[1, 2] and [2, 3]. Median = .{1:1, 2:2, 3:1}. Mode = 2.3: sum=11, count=5. Mean = 2.2. Mode could be 2 or 3 (usually return the smallest or first).median query (), which is too slow for a data stream.This problem is a "composite" challenge. Solve the mean, median, and mode parts independently in your mind first. The Two-Heap pattern for medians is a must-know for any "Stream" or "Statistics" interview question.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Finding MK Average | Hard | Solve | |
| Stock Price Fluctuation | Medium | Solve | |
| Sequentially Ordinal Rank Tracker | Hard | Solve | |
| Implement Router | Medium | Solve | |
| Design a Number Container System | Medium | Solve |