Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Anomaly Detection in Time Series
00:00
5 left

Anomaly Detection in Time Series

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 Anomaly Detection in 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
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.