Flag events more than two standard deviations from their sliding window mean

Quick Overview

Scan a time-sorted list of events and flag each event whose value lies more than two population standard deviations from the mean of same-type events in its trailing time window. It tests per-key sliding windows, running sums, window boundaries and exact handling of the strict threshold.

Flag events more than two standard deviations from their sliding window mean

Company: Splunk

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Design a function to process a stream of events and identify anomalies within a sliding time window. You are given a list of `events` where each event is represented as `[timestamp, event_type, value]`, and an integer `w` representing the time window size. For each event, consider all events of the same type with timestamps in the range `[t - w + 1, t]` (inclusive), where `t` is the current event's timestamp. An anomaly is an event whose value deviates from the mean of the values in its window by more than 2 times the standard deviation (population standard deviation). Formally, an event is anomalous if `|value - mean| > 2 * std_dev` and `std_dev > 0` and there are at least 2 events of the same type in the window. Return a list of all anomalous events as `[timestamp, event_type, value]`, sorted by timestamp. If no anomalies exist, return an empty list. ### Function Signature ```python def process_events(events: list[list], w: int) -> list[list]: ``` ### Rules - The list is processed as a stream. The window of the event at position `i` holds that event plus every earlier event in the list with the same `event_type` and a timestamp of at least `t - w + 1`. Events later in the list are never in the window, even when they share the timestamp `t`. - The mean and the population standard deviation (dividing by the window size) are computed over the values in the window, including the current event's own value. - Only the current event is tested against its window. An earlier event is not tested again when later events join its window. - The comparison `|value - mean| > 2 * std_dev` is strict and exact: a deviation equal to twice the standard deviation is not an anomaly, and no rounding tolerance is applied. - Anomalies are returned in input order, which is non-decreasing by timestamp. ### Constraints - `1 <= len(events) <= 10^4` - Each event is `[timestamp, event_type, value]` - `1 <= timestamp <= 10^9` - `event_type` is a non-empty string of lowercase letters - `-10^6 <= value <= 10^6` - `1 <= w <= 10^9` - Events are sorted by timestamp in non-decreasing order ### Examples ```text Input: events = [[1, "login", 5], [2, "login", 6], [3, "login", 5], [4, "login", 100], [5, "login", 6]], w = 5 Output: [] ``` At timestamp 4 the window holds `[5, 6, 5, 100]`, with mean 29.0 and population standard deviation about 40.99. The deviation of 100 is 71, below the threshold of about 81.99, so it is not an anomaly. At timestamp 5 the window holds all five values (mean 24.4, standard deviation about 37.80), and the current value 6 deviates by only 18.4. ```text Input: events = [[1, "click", 10], [2, "click", 10], [3, "click", 10], [4, "click", 10], [5, "click", 50]], w = 5 Output: [] ``` The first four windows contain only the value 10, so their standard deviation is 0. At timestamp 5 the window is `[10, 10, 10, 10, 50]`, with mean 18.0 and standard deviation 16.0. The deviation `|50 - 18| = 32` equals `2 * 16` and is not strictly greater, so there is no anomaly. ```text Input: events = [[1, "cpu", 10], [2, "cpu", 12], [3, "cpu", 10], [4, "cpu", 12], [5, "cpu", 10], [6, "cpu", 50], [6, "disk", 50], [9, "cpu", 50]], w = 6 Output: [[6, "cpu", 50]] ``` At timestamp 6 the cpu window is `[10, 12, 10, 12, 10, 50]`, with mean about 17.33 and standard deviation about 14.64, so 50 deviates by about 32.67, more than the threshold of about 29.27. The disk event is alone in its own window. At timestamp 9 the cpu window covers timestamps 4 to 9, `[12, 10, 50, 50]`, where 50 deviates by 19.5 against a threshold of about 39.03.

Overview: Scan a time-sorted list of events and flag each event whose value lies more than two population standard deviations from the mean of same-type events in its trailing time window. It tests per-key sliding windows, running sums, window boundaries and exact handling of the strict threshold.

Splunk logo
Splunk
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Design a function to process a stream of events and identify anomalies within a sliding time window.

You are given a list of events where each event is represented as [timestamp, event_type, value], and an integer w representing the time window size. For each event, consider all events of the same type with timestamps in the range [t - w + 1, t] (inclusive), where t is the current event's timestamp.

An anomaly is an event whose value deviates from the mean of the values in its window by more than 2 times the standard deviation (population standard deviation). Formally, an event is anomalous if |value - mean| > 2 * std_dev and std_dev > 0 and there are at least 2 events of the same type in the window.

Return a list of all anomalous events as [timestamp, event_type, value], sorted by timestamp. If no anomalies exist, return an empty list.

Function Signature

def process_events(events: list[list], w: int) -> list[list]:

Rules

  • The list is processed as a stream. The window of the event at position i holds that event plus every earlier event in the list with the same event_type and a timestamp of at least t - w + 1 . Events later in the list are never in the window, even when they share the timestamp t .
  • The mean and the population standard deviation (dividing by the window size) are computed over the values in the window, including the current event's own value.
  • Only the current event is tested against its window. An earlier event is not tested again when later events join its window.
  • The comparison |value - mean| > 2 * std_dev is strict and exact: a deviation equal to twice the standard deviation is not an anomaly, and no rounding tolerance is applied.
  • Anomalies are returned in input order, which is non-decreasing by timestamp.

Constraints

  • 1 <= len(events) <= 10^4
  • Each event is [timestamp, event_type, value]
  • 1 <= timestamp <= 10^9
  • event_type is a non-empty string of lowercase letters
  • -10^6 <= value <= 10^6
  • 1 <= w <= 10^9
  • Events are sorted by timestamp in non-decreasing order

Examples

Input: events = [[1, "login", 5], [2, "login", 6], [3, "login", 5], [4, "login", 100], [5, "login", 6]], w = 5
Output: []

At timestamp 4 the window holds [5, 6, 5, 100], with mean 29.0 and population standard deviation about 40.99. The deviation of 100 is 71, below the threshold of about 81.99, so it is not an anomaly. At timestamp 5 the window holds all five values (mean 24.4, standard deviation about 37.80), and the current value 6 deviates by only 18.4.

Input: events = [[1, "click", 10], [2, "click", 10], [3, "click", 10], [4, "click", 10], [5, "click", 50]], w = 5
Output: []

The first four windows contain only the value 10, so their standard deviation is 0. At timestamp 5 the window is [10, 10, 10, 10, 50], with mean 18.0 and standard deviation 16.0. The deviation |50 - 18| = 32 equals 2 * 16 and is not strictly greater, so there is no anomaly.

Input: events = [[1, "cpu", 10], [2, "cpu", 12], [3, "cpu", 10], [4, "cpu", 12], [5, "cpu", 10], [6, "cpu", 50], [6, "disk", 50], [9, "cpu", 50]], w = 6
Output: [[6, "cpu", 50]]

At timestamp 6 the cpu window is [10, 12, 10, 12, 10, 50], with mean about 17.33 and standard deviation about 14.64, so 50 deviates by about 32.67, more than the threshold of about 29.27. The disk event is alone in its own window. At timestamp 9 the cpu window covers timestamps 4 to 9, [12, 10, 50, 50], where 50 deviates by 19.5 against a threshold of about 39.03.