Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Sliding Window Moving Average

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Sliding Window Moving Average. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Arista EOS telemetry can produce a sequence of numeric samples such as interface utilization or packet rates. Given the samples and a fixed window size, return the average for every contiguous window of that size.

Implement moving_average(nums, k) using a sliding window. Each output value must equal the arithmetic mean of one window, starting with nums[0:k] and advancing one position at a time.

Formal Specification

  • Input: nums, a non-empty list of integers or floating-point numbers, and k, a positive integer window size.
  • Output: A list of floating-point averages with length len(nums) - k + 1.
  • Windows must preserve the original order and may overlap.

Constraints

  • 1 <= k <= len(nums) <= 10^5
  • -10^6 <= nums[i] <= 10^6
  • The output contains exactly len(nums) - k + 1 values
  • Standard floating-point division is used

Function Signature

def moving_average(nums, k):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output