Moving Average from Data Stream

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.

Input Format

values = stream values, size = window size

Output Format

moving average after each insertion

Constraints

  • 1 <= input size <= 10^5
  • -10^9 <= numeric values <= 10^9
  • values, size must satisfy the format described in inputFormat.

Examples

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.

Loading...
Moving Average from Data Stream - Queue Deque