Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Moving Average With Missing Values

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

Your question is Moving Average With Missing Values. 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

Avenue Code's ML pipelines may receive time-series measurements with missing samples. Given an ordered sequence of numeric values, compute the trailing moving average for every position while ignoring missing values.

Use None to represent a missing sample. For each index, consider the current value and up to window - 1 preceding values. Average only the non-missing values in that window. If every value in a window is missing, return None for that position.

Formal Specification

Implement moving_average(values, window), where values is a list containing integers, floating-point numbers, or None, and window is a positive integer. Return a list of the same length containing floating-point averages or None.

Constraints

  • 1 <= len(values) <= 10^5
  • 1 <= window <= len(values)
  • Each non-missing value is between -10^9 and 10^9
  • Missing values are represented only by None

Function Signature

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