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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
Anomaly Detection in Sensor Streams
00:00
5 left

Anomaly Detection in Sensor Streams

MediumPython

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):
Interviewer

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