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
Implement Rolling Median MAD Anomaly Detection
00:00
5 left

Implement Rolling Median MAD Anomaly Detection

HardPython

Problem

Tudor Investment's high-frequency research pipeline needs a deterministic filter for unusually large observations in an ordered stream of market values. Implement a robust rolling anomaly detector using the trailing median and median absolute deviation, or MAD.

For each observation after the first window values, use only the previous window observations as the reference distribution. Mark the current observation as anomalous when its absolute deviation from the reference median is strictly greater than threshold * MAD.

If the MAD is zero, any current value different from the median is anomalous. Observations without a complete preceding window cannot be classified and must not be returned.

Formal Specification

Implement identify_anomalies(values, window, threshold). values is a list of finite numbers ordered by arrival time. window is an integer, and threshold is a positive number. Return a list of zero-based indices identified as anomalies, in ascending order.

The median of an even-length list is the average of its two middle values. The MAD is the median of the absolute deviations from the median.

Constraints

  • 1 <= window < len(values) <= 10^4
  • threshold > 0
  • Values are finite real numbers
  • The output contains zero-based indices in ascending order

Function Signature

def identify_anomalies(values, window, threshold):
Interviewer

Your question is Implement Rolling Median MAD Anomaly Detection. 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.