Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Anomaly Detection in Sensor Streams

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

Your question is Anomaly Detection in Sensor Streams. 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

NetApp ONTAP can expose ordered telemetry readings such as latency, temperature, or throughput. Given a stream of numeric readings, identify readings that differ significantly from the recent baseline.

For each reading, compare it with the median of the previous window_size readings, or all previous readings when fewer than window_size exist. Mark the reading as anomalous when its absolute difference from that median is strictly greater than threshold. Do not evaluate the first reading because it has no prior baseline. Return the zero-based indices of all anomalous readings.

Formal Specification

Implement detect_anomalies(data, window_size, threshold). The input data is a list of integers or floating-point values. window_size is a positive integer, and threshold is a nonnegative number. Return a list of integer indices in ascending order. The rolling window contains every prior reading, including readings previously identified as anomalous.

For an even-sized window, define the median as the average of the two middle values.

Constraints

  • 1 <= len(data) <= 100,000
  • 1 <= window_size <= len(data)
  • 0 <= threshold <= 10^9
  • -10^9 <= data[i] <= 10^9
  • The rolling window includes previously detected anomalies

Function Signature

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