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
Real-Time Moving Metrics Structure
00:00
5 left

Real-Time Moving Metrics Structure

HardPython

Problem

AKUNA Capital's real-time trading components need moving statistics for a stream of integer market prices. Given a fixed window size, process each arriving price and return the current mean and mode of the prices currently in the window.

The window contains at most the most recent window_size prices. For the mode, return the value with the highest frequency. If multiple values have the same highest frequency, return the smallest value.

Formal Specification

Implement moving_metrics(prices, window_size), where prices is a list of integers and window_size is a positive integer. Return a list of dictionaries, one per arriving price, with the exact keys mean and mode. The mean must be returned as a Python floating-point value. Before the window reaches window_size, calculate metrics over all prices seen so far.

An efficient solution should avoid rescanning the complete window after every insertion.

Constraints

  • 1 <= len(prices) <= 10^5
  • 1 <= window_size <= len(prices)
  • -10^9 <= prices[i] <= 10^9
  • All prices are integers

Function Signature

def moving_metrics(prices, window_size):
Interviewer

Your question is Real-Time Moving Metrics Structure. 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.