Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Metrics Anomaly Detection

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

Your question is Metrics Anomaly Detection. 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

Zoox vehicle telemetry streams contain metrics such as battery temperature, latency, and sensor voltage. Implement a function that identifies anomalous readings using a rolling Hampel filter, without allowing the current reading to influence its own baseline.

For each reading, compare it with the preceding window_size readings, or all preceding readings if fewer are available. Do not evaluate a reading until at least three preceding readings exist. Let m be the window median and MAD be the median of the absolute deviations from m. Define the robust scale as 1.4826 * MAD. A reading is anomalous when its absolute difference from m is greater than threshold * robust_scale. If MAD is zero, any reading different from the median is anomalous.

Return the zero-based indices of anomalous readings in chronological order. The input metric stream is not modified.

Formal Specification

  • Input: metrics, a list of integers or floats; window_size, an odd integer; and positive float threshold.
  • Output: A list of integer indices.

Constraints

  • 0 <= len(metrics) <= 100000
  • 3 <= window_size <= 1001
  • window_size is odd
  • 0 < threshold <= 20
  • Metric values fit within signed 64-bit range
  • At least three preceding readings are required for evaluation

Function Signature

def detect_anomalies(metrics, window_size, threshold):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output