Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Normalize Sensor Data And Detect Anomalies

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

Your question is Normalize Sensor Data And Detect Anomalies. 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

MarshBerry's monitoring pipeline receives chronological sensor readings as timestamped numeric values. Implement a function that min-max normalizes all readings and identifies values that are anomalous compared with the preceding readings.

For each reading, use up to the previous window readings as its baseline. A reading is anomalous when the baseline contains at least two values and either the baseline standard deviation is zero while the current value differs from the baseline mean, or the absolute difference from the baseline mean is greater than threshold * standard_deviation. Use population standard deviation. The current reading must not be included in its own baseline.

Return a dictionary with normalized, a list of normalized values rounded to four decimal places, and anomalies, a list of zero-based indices. Normalize using (value - minimum) / (maximum - minimum). If all values are equal, every normalized value is 0.0.

Formal Specification

Input readings is a non-empty list of dictionaries with integer timestamp and numeric value fields. Input timestamps are chronological. window is a positive integer, and threshold is a positive number. Return {"normalized": list[float], "anomalies": list[int]}.

Constraints

  • 1 <= len(readings) <= 10^5
  • 1 <= window <= len(readings)
  • threshold > 0
  • -10^9 <= reading['value'] <= 10^9
  • Timestamps are integers in nondecreasing order

Function Signature

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