Spike detection anomalies

Write codeMedium25 minDatabricks and Palantir style

Your task

Given a numeric stream and a window size, flag anomaly indices. For each index i with at least window prior values, compute the mean and population standard deviation of the immediately preceding window values (indices i-window .. i-1); index i is a spike if stream[i] > mean + 2*std. Return the list of spike indices in increasing order. Indices before a full trailing window exists are never flagged.

Examples

detect_spikes(stream: list[int], window: int) -> list[int]
Example 1
Inputdetect_spikes([1, 1, 1, 1, 50], 4)
Output[4]