Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Time-Series Metrics Function
00:00
5 left

Time-Series Metrics Function

HardPython

Problem

Zoox telemetry can contain a time-ordered stream of measurements such as planning latency or actuator delay. Implement a one-pass function that summarizes the stream without materializing all samples.

Each sample is a (timestamp, value) pair. The value at sample i is considered active from timestamp[i] until timestamp[i + 1]; the final sample contributes no duration because its ending time is unknown.

Return a dictionary containing the sample count, arithmetic mean, population standard deviation, minimum, maximum, total observed duration, duration spent strictly above a supplied threshold, the fraction of observed duration above that threshold, and the longest consecutive above-threshold duration. For an empty stream, return None for value-based metrics and 0 for duration-based metrics.

Formal Specification

Implement stream_metrics(samples, threshold), where samples is an iterable of strictly increasing numeric timestamps and numeric values, and threshold is numeric. Return a dictionary with keys count, mean, stddev, min, max, observed_duration, above_threshold_duration, above_threshold_fraction, and longest_above_threshold_duration.

Use population standard deviation. A value equal to threshold is not above the threshold.

Constraints

  • samples contains zero or more samples.
  • Timestamps are strictly increasing numeric values.
  • Values and threshold are finite numeric values.
  • The function must process the iterable in one pass.
  • The algorithm must use O(1) additional space.

Function Signature

def stream_metrics(samples, threshold):
Interviewer

Your question is Time-Series Metrics Function. 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
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.