Mockbit/#73
ML CodingmediumNumpy~15m

Moving Average

Problem

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.

Examples

Example 1

Input: ([1, 2, 3, 4, 5, 6], 4)
Output: [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

Input: ([10, 20, 30, 40], 4)
Output: [25.0]

k equals len(arr) — one window covers the entire array. Output has exactly 1 element.

Example 3

Input: ([1000, 1000, 1000, 999], 3)
Output: [1000.0, 999.666667]

Second window: (1000+1000+999)/3 = 999.666... rounded to 6 decimal places.

Constraints
  • 1 <= k <= len(arr) <= 1000
  • -1000 <= arr[i] <= 1000
Reference solution

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.

Related ML Coding questions
← Back homemockbit.io/q/73
PrivacyTerms© 2026 Mockbit