Moving average (integer)
A free easy coding practice problem from data. Solve it in JavaScript, TypeScript or Python and check your solution against fresh hidden tests — edge cases and random inputs — as often as you like.
- Difficulty
- Easy
- Type
- Function
- Topic
- Data
- Languages
- JavaScript, TypeScript or Python
Problem
For a metric series nums (non-negative integers ≤ 10^6) return the moving average over every window of k consecutive values, in order, each rounded DOWN to an integer: element i of the result is floor((nums[i] + … + nums[i+k−1]) / k). If k is larger than the length of nums, return []. 1 ≤ k, up to 200 000 values — compute each window in O(1).
Function signature
// JavaScript
function movingAverageFloor(nums, k)
// TypeScript
function movingAverageFloor(nums: any, k: any): any
// Python
def moving_average_floor(nums, k):Examples
movingAverageFloor([1,2,3,4,5], 2)→[1,2,3,4]movingAverageFloor([10,0,0,7], 3)→[3,2]movingAverageFloor([5], 2)→[]
How checking works
Run your code against the examples above, then submit a check: it runs on the server against hidden tests built fresh each time, including edge cases and random inputs. Practice is untimed and never affects a verified score; when you are ready, take a graded coding challenge to earn a badge.