Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
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
ExperiencesCoaching
New
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
Get started
Running Average with Fixed Window
00:00
5 left

Running Average with Fixed Window

MediumPython

Problem

Write a function to parse a stream of real-time sensor readings and calculate a running average using a fixed-size window.

Implement running_average(readings, window_size). readings is an iterable of numeric values, and the function must return a list containing one average after each reading. During warm-up, average all readings received so far; afterward, average only the most recent window_size readings. Preserve input order and return an empty list for an empty stream.

Constraints

  • 0 <= len(readings) <= 100000
  • 1 <= window_size
  • Each reading is a finite integer or floating-point value
  • Return one floating-point average per input reading

Function Signature

def running_average(readings, window_size):
Interviewer

Your question is Running Average with Fixed Window. 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.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.