Skip to content
RED SAGSkills

Sensor Moving Average

A sensor produces readings nums. For every block of k consecutive readings, compute the average. Return the averages in order, each rounded to 2 decimal places (use Math.round(x * 100) / 100).

If there are fewer than k readings, no full window exists and you return an empty array. Keep a running sum so each step is constant time.

Example 1

Input: nums = [1,2,3,4,5], k = 2
Output: [1.5,2.5,3.5,4.5]

Example 2

Input: nums = [1,2,2], k = 3
Output: [1.67]

Constraints

  • 0 <= nums.length <= 10^5
  • 1 <= k <= 10^5
  • -10^4 <= nums[i] <= 10^4
Hint 1

Add the new reading to a running sum and subtract the one that leaves the window.

Hint 2

Only start recording once the window is full.

Good practice for: Data Scientist, Machine Learning Engineer, Data Engineer, Embedded Systems Engineer, Site Reliability Engineer, AI Engineer (LLM & Generative AI)

Run checks your code on 3 sample tests. Submit runs all 8 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.