Moving Average Clicks
An ad analytics dashboard displays a smoothed click-through count using a fixed-size moving average. Each minute it appends the latest count; it shows the rolling mean of the last w minutes (or fewer, if not enough data has been collected yet).
Problem
Implement a class MovingAverage(window_size) with one method push(value) that returns the average of at most the last window_size values pushed so far. Until window_size pushes have been made, return the average of everything pushed.
Input
Constructor takes window_size (1 ≤ w ≤ 10^4). push receives an integer value (0 ≤ value ≤ 10^6) and may be called up to 10^5 times.
Output
Each push returns a double — the current rolling mean.
Constraints
- 1 ≤ window_size ≤ 10,000
- Up to 100,000 pushes
- O(1) amortised per push — recomputing the mean from scratch every time is too slow
Examples
Example 1
Input
ma = MovingAverage(3); ma.push(1); ma.push(2); ma.push(3); ma.push(4)
Output
1.0, 1.5, 2.0, 3.0
After 1: mean(1)=1. After 2: mean(1,2)=1.5. After 3: mean(1,2,3)=2. After 4: window slides → mean(2,3,4)=3.