Report the most frequent value in a sliding time window and flag majority anomalies

Quick Overview

For each event in a timestamp-sorted stream, find the most frequent value inside a sliding time window, break ties by the smallest value, and negate it when it holds a strict majority. Tests two-pointer window upkeep and tracking the most frequent value as events enter and leave.

Report the most frequent value in a sliding time window and flag majority anomalies

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a stream of events as an array `events`, where each element is `[timestamp, value]` and the array is sorted by timestamp in non-decreasing order. You are also given an integer `w`, the duration of a time window. For each event at index `i` with timestamp `t`, look at the events in the sliding time window `[t - w + 1, t]` (inclusive on both ends) and find the most frequent value in that window. If several values tie on frequency, choose the smallest of them. Also detect anomalies: if the most frequent value appears strictly more than half as often as the number of events in the window (`frequency > window_count / 2`) and the window contains more than one event, report the negation of that value. Otherwise report the value as it is. Return a list with one result per event, in input order. ### Function Signature ```python def process_stream(events: list[list[int]], w: int) -> list[int]: ``` ### Rules - The window for event `i` holds the events at indices `0` to `i` whose timestamps lie in `[t - w + 1, t]`, where `t` is the timestamp of event `i`. An event later in the array is not in the window yet, even when it has the same timestamp `t`. - `window_count` is the number of events in that window, counting repeated values separately. It is always at least 1, because event `i` is in its own window. - "Smallest value" uses ordinary signed order, so `-5` is smaller than `3`. Negating a negative value gives a positive result, and `0` is reported as `0` either way. - A window with exactly one event is never flagged. ### Constraints - `1 <= len(events) <= 10^5` - `1 <= timestamp <= 10^9` - Timestamps are sorted in non-decreasing order. - `-10^4 <= value <= 10^4` - `1 <= w <= 10^9` ### Examples ```text Input: events = [[1, 3], [2, 3], [3, 5], [4, 3], [5, 5]], w = 3 Output: [3, -3, -3, -3, -5] ``` With `w = 3` the window at time `t` covers timestamps `t - 2` to `t`. The first window has a single event, and every later window has one value holding at least 2 of its 2 or 3 events (3 at times 2 to 4, then 5 at time 5). ```text Input: events = [[10, 1], [20, 2], [30, 3], [40, 4]], w = 5 Output: [1, 2, 3, 4] ``` The timestamps are 10 apart and `w = 5`, so each window holds only its own event, and a single-event window is never flagged. ```text Input: events = [[1, 4], [2, 1], [3, 4], [3, 1]], w = 10 Output: [4, 1, -4, 1] ``` Every window reaches back to the first event. Index 2 does not yet include index 3 (same timestamp), so 4 holds 2 of 3 events and is flagged, while at index 1 (one 4, one 1) and index 3 (two of each) the tie goes to 1, which holds exactly half and is not flagged.

Overview: For each event in a timestamp-sorted stream, find the most frequent value inside a sliding time window, break ties by the smallest value, and negate it when it holds a strict majority. Tests two-pointer window upkeep and tracking the most frequent value as events enter and leave.

Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a stream of events as an array events, where each element is [timestamp, value] and the array is sorted by timestamp in non-decreasing order. You are also given an integer w, the duration of a time window.

For each event at index i with timestamp t, look at the events in the sliding time window [t - w + 1, t] (inclusive on both ends) and find the most frequent value in that window. If several values tie on frequency, choose the smallest of them.

Also detect anomalies: if the most frequent value appears strictly more than half as often as the number of events in the window (frequency > window_count / 2) and the window contains more than one event, report the negation of that value. Otherwise report the value as it is.

Return a list with one result per event, in input order.

Function Signature

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

Rules

  • The window for event i holds the events at indices 0 to i whose timestamps lie in [t - w + 1, t] , where t is the timestamp of event i . An event later in the array is not in the window yet, even when it has the same timestamp t .
  • window_count is the number of events in that window, counting repeated values separately. It is always at least 1, because event i is in its own window.
  • "Smallest value" uses ordinary signed order, so -5 is smaller than 3 . Negating a negative value gives a positive result, and 0 is reported as 0 either way.
  • A window with exactly one event is never flagged.

Constraints

  • 1 <= len(events) <= 10^5
  • 1 <= timestamp <= 10^9
  • Timestamps are sorted in non-decreasing order.
  • -10^4 <= value <= 10^4
  • 1 <= w <= 10^9

Examples

Input: events = [[1, 3], [2, 3], [3, 5], [4, 3], [5, 5]], w = 3
Output: [3, -3, -3, -3, -5]

With w = 3 the window at time t covers timestamps t - 2 to t. The first window has a single event, and every later window has one value holding at least 2 of its 2 or 3 events (3 at times 2 to 4, then 5 at time 5).

Input: events = [[10, 1], [20, 2], [30, 3], [40, 4]], w = 5
Output: [1, 2, 3, 4]

The timestamps are 10 apart and w = 5, so each window holds only its own event, and a single-event window is never flagged.

Input: events = [[1, 4], [2, 1], [3, 4], [3, 1]], w = 10
Output: [4, 1, -4, 1]

Every window reaches back to the first event. Index 2 does not yet include index 3 (same timestamp), so 4 holds 2 of 3 events and is flagged, while at index 1 (one 4, one 1) and index 3 (two of each) the tie goes to 1, which holds exactly half and is not flagged.