PIXELBANKv9.1.0
Menu

Moving Average from Data Stream

Given a stream of integers and a window size k, calculate the moving average of the last k values each time a new value arrives.

You will receive the window size and a sequence of values. For each value, output the moving average with 2 decimal places.

Example:

Input:
3
1,10,3,5
Output:
1.00
5.50
4.67
6.00
Reasoning:
  • Initially, the window size k is set to 3, and the first value 1 arrives, so the moving average is 1.00=111.00 = \frac{1}{1}.
  • When the second value 10 arrives, the window contains [1, 10], so the moving average is 1+102=5.50\frac{1+10}{2} = 5.50.
  • The third value 3 arrives, and the window becomes [1, 10, 3], so the moving average is 1+10+33=4.67\frac{1+10+3}{3} = 4.67.
  • Finally, the fourth value 5 arrives, and the window updates to [10, 3, 5], so the moving average is 10+3+53=6.00\frac{10+3+5}{3} = 6.00.

Constraints:

  • 1 <= k <= 1000
  • -10^5 <= val <= 10^5
  • At most 10^4 calls to next
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Moving Average from Data Stream - Easy | PixelBank