Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling Averages and Outliers

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

Your question is Rolling Averages and Outliers. 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

Amazon CloudWatch receives telemetry records ordered by timestamp. Given a time-series of (timestamp, value) pairs, calculate the rolling average and determine whether each value is a statistical outlier compared with earlier values in the active time window.

For each record, use earlier records whose timestamps satisfy timestamp > current_timestamp - window_seconds as the baseline. Compute the population mean and standard deviation of that baseline. A value is an outlier when there are at least min_history baseline records and its absolute deviation from the baseline mean is greater than z_threshold * standard_deviation. If the baseline standard deviation is zero, any different value is an outlier. The rolling average includes the current value and the active earlier values.

Return one list per input record: [timestamp, rolling_average_rounded_to_2_decimals, is_outlier]. The first value is never an outlier if insufficient history exists.

Formal Specification

Implement process_telemetry(records, window_seconds, z_threshold, min_history), where records is a nondecreasing list of [integer_timestamp, numeric_value] pairs. Return a list of [integer, float, boolean] lists in the same order.

Constraints

  • 0 <= len(records) <= 10^6
  • Timestamps are nondecreasing integers
  • Values are finite numbers
  • 1 <= window_seconds <= 10^9
  • 0 < z_threshold <= 10^3
  • 1 <= min_history <= 10^6

Function Signature

def process_telemetry(records, window_seconds, z_threshold, min_history):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output