Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Moving Average for Time Series
00:00
5 left

Moving Average for Time Series

EasyPython

Problem

Castleton Commodities International uses rolling statistics to monitor market time series. Given a sequence of numeric observations and a window size, return the moving average at every position.

For position i, average the values from max(0, i - window + 1) through i. Initial positions use the values available so far, so the output has the same length as the input. Return an empty list when the input series is empty.

Formal Specification

Implement moving_average(values, window).

  • Input: values, a list of integers or floating-point numbers, and window, a positive integer.
  • Output: A list of floating-point averages, with one result for each input value.
  • The function must not modify values.

Constraints

  • 0 <= len(values) <= 10^5
  • 1 <= window <= len(values) when values is non-empty
  • -10^9 <= values[i] <= 10^9
  • The input list must not be modified

Function Signature

def moving_average(values, window):
Interviewer

Your question is Moving Average for Time Series. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.