Given a stream of integers and a fixed window size, return the moving average after each insertion. Pattern focus: Circular Queue (Ring Buffer). Keep a rolling sum and overwrite the oldest element when the buffer is full.
values = stream values, size = window size
moving average after each insertion
Example 1:
Input:
values = [1,10,3,5] size = 3
Output:
[1.0,5.5,4.6666666667,6.0]
Explanation:
The average uses the last up to 3 values at each step.
Example 2:
Input:
values = [4,0,-1] size = 2
Output:
[4.0,2.0,-0.5]
Explanation:
The buffer drops the oldest value once full.