Skip to content
Coding · Hard · Alerting · Target O(n), amortised O(1) per event · Senior

Deduplicate alerts with flap suppression

Collapse a stream of alert events by fingerprint: drop repeats inside a quiet period, and report an alert as flapping once when it changes state too often in a window.

Statement

A monitoring system re-sends every alert on each evaluation, so the pager receives a stream of events (ts, fingerprint, state), with state "firing" or "resolved", ordered by ts in seconds. Write alert_notifications(events, quiet_s, flap_window_s, flap_threshold) that returns the notifications worth sending as (ts, fingerprint, kind), in event order, where kind is "firing", "resolved" or "flapping". Each fingerprint is handled on its own and starts resolved.

A state change is notified with its new state. A repeated firing is notified again only when at least quiet_s seconds have passed since that fingerprint's last notification of any kind. A repeated resolved is never notified.

An alert is flapping at an event when at least flap_threshold of its state changes, counting this event if it is one, have a timestamp in (ts − flap_window_s, ts]. The event at which flapping starts is notified once as flapping, and nothing else is notified while it lasts. The first event after flapping has stopped is notified with its own state, whether or not it is a change, so the pager learns where the alert settled.

Examples

quiet_s = 300, flap_window_s = 600, flap_threshold = 4, one fingerprint

t=0     firing    → firing     # first state change
t=60    firing                 # repeat inside the quiet period
t=300   firing    → firing     # 300 s since the last notification
t=360   resolved  → resolved
t=400   firing    → firing
t=450   resolved  → flapping   # 4th change in 600 s: 0, 360, 400, 450
t=500   firing                 # still flapping
t=700   firing                 # 4 changes in (100, 700]: still flapping
t=1200  firing    → firing     # no changes in (600, 1200]: settled

Constraints

Up to 2 × 10⁵ events. quiet_s ≥ 1, flap_window_s ≥ 1 and flap_threshold ≥ 2. Events with equal ts are handled in input order. A change exactly flap_window_s seconds old is outside the window. A resolved event for a fingerprint that has never fired is a repeat. Each event must cost amortised O(1).

Open to everyone until Pro goes on sale · sign in to save your attemptSolve it in the editor with visible tests
Open in the app