PIXELBANKv9.1.0
Menu

Alert Deduplication with Flap Suppression

Problem Statement

Process a stream of alert state changes and count how many notifications should actually be sent, suppressing rapid flapping using a dampening window.

Background

Alerts fire ("firing") and resolve ("resolved"). A notification is sent when an alert transitions into firing from a non-firing state — but only if at least cooldown seconds have passed since the last notification for that alert key. Transitions to resolved never notify (in this model). Repeated firing events while already firing do not notify. Each event is (key, state, time), processed in time order.

Your Task

def count_notifications(events, cooldown):

Return the number of notifications sent (int).

Input Format

  • events: list of (key, state, time), time non-decreasing; cooldown (int).

Output Format

  • A single int.

Sample

evts = [("db","firing",0),("db","resolved",5),("db","firing",100)]
print(count_notifications(evts, 60))

Output:

2

Example:

Input:
evts = [("db","firing",0),("db","resolved",5),("db","firing",100)]
print(count_notifications(evts, 60))
Output:
2
Reasoning:
  • Process the first event ("db", "firing", 0): The alert transitions from its initial non-firing state to firing. Since no previous notification exists for key "db", the cooldown check passes, triggering the first notification and recording the timestamp t=0t=0.
  • Process the second event ("db", "resolved", 5): The state changes to resolved. As transitions to resolved never generate notifications, the count remains unchanged and no new timestamp is recorded.
  • Process the third event ("db", "firing", 100): The alert transitions back to firing. We verify the dampening window by calculating the elapsed time since the last notification: 100−0=100100 - 0 = 100.
  • Compare the elapsed time against the cooldown threshold: Since 100≥60100 \ge 60, the suppression condition is not met, so a second notification is sent and the last notification timestamp is updated to 100100.
  • The final output is 2

Constraints:

  • Notify only on a transition into firing from non-firing.
  • Suppress if time - last_notify[key] < cooldown.
  • Track per-key current state and last notify time; resolved never notifies.
solution.py

Test Results

0/0
Run code to see test results.