Code RoomSpike detection anomalies
MediumPrep Room Coding #1440

Spike detection anomalies

CodingDistributed systemsAlgorithms & data structuresMid–Senior~25 min

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.

Implement
detect_spikes(stream: list[int], window: int) → list[int]
Examples
in[[1,1,1,1,50],4]out[4]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 25 min
InputExpectedGot
[[1,1,1,1,50],4][4]not run yetsample