Moving Average
You are building a time series preprocessing pipeline for a machine learning model. Raw sensor readings are noisy — you need to smooth them using a sliding window average before training.
Given a 1D array of numbers and a window size k, return a new array where each element is the average of k consecutive elements. The output has length len(arr) - k + 1.
Round each output value to 6 decimal places.
Example 1
([1, 2, 3, 4, 5, 6], 4)[2.5, 3.5, 4.5]Windows: [1,2,3,4]→2.5, [2,3,4,5]→3.5, [3,4,5,6]→4.5. Output length = 6-4+1 = 3.
Example 2
([10, 20, 30, 40], 4)[25.0]k equals len(arr) — one window covers the entire array. Output has exactly 1 element.
Example 3
([1000, 1000, 1000, 999], 3)[1000.0, 999.666667]Second window: (1000+1000+999)/3 = 999.666... rounded to 6 decimal places.
- ›1 <= k <= len(arr) <= 1000
- ›-1000 <= arr[i] <= 1000
Reference solution available after you attempt the question.
Ready to solve it?
Start a session on Mockbit #73. Write your code, run it against hidden tests, and get graded with specific critique on each axis.