Process merchant transaction logs and output alerts when failures with the same error code spike within a 30-second sliding window.
You receive a vector of transactions and need to output a vector of alerts. For each transaction, check whether an alert should be triggered. All evaluations use a 30-second sliding window: for a log at timestamp T, the window includes all logs in the range [T-29, T]. Alerts are triggered per {merchantId, code} pair. Each input contains a status code, and everything except 200 counts as an error. Output strings have the format timestamp,event_type,merchant_id,status_code, sorted by (timestamp ASC, merchant_id ASC, status_code ASC, event_type ASC). If an alert was triggered and is still active (not resolved), do not trigger a duplicate alert.
Part 1: Trigger an alert if there are at least 5 failures with the exact same error code for the same merchant within the last 30 seconds.
Input: 10,merchant1,500,2 10,merchant2,500,1 15,merchant1,500,2 20,merchant1,500,1 20,merchant2,500,4
Output: 20,TRIGGER,merchant1,500 20,TRIGGER,merchant2,500
Part 2: Trigger only if both conditions are met: (1) at least 5 failures with the exact same error (the part 1 condition), and (2) those failures represent more than 1% of the merchant's successful transaction volume (the total of 200s for that merchant in the same window).
Input: 10,merchant1,200,600 12,merchant1,500,6 15,merchant2,200,599 16,merchant2,500,6
Output: 16,TRIGGER,merchant2,500
Part 3: Trigger conditions are the same as parts 1 and 2. Now implement RESOLVE: (1) an alert is resolved at timestamp T if a log arrives for that merchant at T and the 30-second window ending at T no longer meets the trigger conditions; (2) an alert can only resolve when a log is processed for that merchant; (3) once an alert is resolved, it can be triggered again.
Input: 10,merchant1,500,6 16,merchant2,500,6 45,merchant1,200,1 45,merchant2,200,1
Output: 10,TRIGGER,merchant1,500 16,TRIGGER,merchant2,500 45,RESOLVE,merchant1,500
Expect follow-ups like
- Now trigger only when there are at least 5 failures with the same error code and those failures are more than 1% of the merchant's successful (status 200) volume in the same window.
- Now implement RESOLVE: an active alert resolves at timestamp T when a log for that merchant arrives at T and the 30-second window ending at T no longer meets the trigger conditions; once resolved, it can trigger again.
Format · 1 hour; 3 parts; about 3 screens of problem text; tests had to pass for every part.
Community answers
Related courses


