Problem Description
Design a class to calculate the moving average of a stream of integers. The class is initialized with a fixed window size, and each new number from the stream is added to this window. If the window exceeds its size, the oldest number is removed. The moving average is then the sum of the numbers in the window divided by the number of elements in the window.
Key Insights
- Use a queue (or circular buffer) to keep track of the current window elements.
- Maintain a running sum to quickly update the window's total when a new element comes in.
- When the window is full, subtract the value of the element that falls out as a new one is added.
- Each call to next() is executed in constant time, O(1).
Space and Time Complexity
Time Complexity: O(1) per call to next()
Space Complexity: O(window size)
Solution
The solution leverages a queue data structure to store the elements of the sliding window. A running sum variable is maintained for efficient calculation of the moving average. When a new integer is added:
- If the size of the queue is equal to the maximum window size, remove the oldest element and subtract its value from the running sum.
- Add the new element to both the queue and the running sum.
- Compute and return the moving average as the running sum divided by the number of elements in the queue. This approach ensures that each update is done in constant time without having to sum all elements in the window repeatedly.