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.
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.
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.
def process_telemetry(records, window_seconds, z_threshold, min_history):