Alert Engine with a Repeat Interval
Updated
Problem statement
An alert engine runs n alerts. Alert i runs its metric query every intervals[i] seconds, at times 0, intervals[i], 2 · intervals[i], and so on. Its j-th query (counting from 0) runs at time j · intervals[i] and returns values[i][j]; the alert runs exactly values[i].length queries. Every alert starts out resolved.
After each query, the alert compares the returned value with thresholds[i]:
- If the value is greater than the threshold and the alert is resolved, it starts firing and sends a notification.
- If the value is greater than the threshold and the alert is already firing, it sends another notification only when
repeatIntervals[i] > 0and at leastrepeatIntervals[i]seconds have passed since its previous notification. WhenrepeatIntervals[i] == 0, a firing alert never repeats. - If the value is less than or equal to the threshold and the alert is firing, it becomes resolved and sends a resolve message. If it is already resolved, nothing is sent.
Write each message as "<time> <alert> NOTIFY" or "<time> <alert> RESOLVE", for example "15 0 NOTIFY". Return all messages ordered by time, and messages with the same time by alert index.
Function
runAlertEngine(intervals: int[], thresholds: int[], repeatIntervals: int[], values: int[][]) → String[]Examples
Example 1
intervals = [10]thresholds = [50]repeatIntervals = [0]values = [[40,60,70,50,55,30]]return = ["10 0 NOTIFY","30 0 RESOLVE","40 0 NOTIFY","50 0 RESOLVE"]The alert queries at times 0, 10, 20, 30, 40 and 50.
- At time 0 the value 40 is not above 50, and the alert is already resolved, so nothing is sent.
- At time 10 the value 60 is above 50, so the alert starts firing and sends
"10 0 NOTIFY". - At time 20 it is still firing. Its repeat interval is 0, so no second notification is sent.
- At time 30 the value 50 equals the threshold, which is not above it, so the alert resolves:
"30 0 RESOLVE". - At time 40 the value 55 starts a new firing period, which always notifies:
"40 0 NOTIFY". At time 50 it resolves again.
Example 2
intervals = [5]thresholds = [100]repeatIntervals = [12]values = [[120,130,140,150,160,90]]return = ["0 0 NOTIFY","15 0 NOTIFY","25 0 RESOLVE"]The alert fires at time 0 and notifies. At times 5 and 10 only 5 and 10 seconds have passed since that notification, less than the repeat interval 12. At time 15, 15 seconds have passed, so it notifies again and the count restarts from 15. At time 20 only 5 seconds have passed. At time 25 the value 90 is not above 100, so it resolves.
Example 3
intervals = [3,2]thresholds = [0,5]repeatIntervals = [6,0]values = [[1,1,1,-1],[9,9,2,9]]return = ["0 0 NOTIFY","0 1 NOTIFY","4 1 RESOLVE","6 0 NOTIFY","6 1 NOTIFY","9 0 RESOLVE"]Alert 0 queries at 0, 3, 6 and 9: it notifies at 0, stays quiet at 3 (3 seconds since the last notification), notifies again at 6 (6 seconds, which reaches the repeat interval) and resolves at 9. Alert 1 queries at 0, 2, 4 and 6: it notifies at 0, resolves at 4 when the value drops to 2, and notifies at 6 when it fires again.
Messages at the same time are listed by alert index, so "0 0 NOTIFY" comes before "0 1 NOTIFY" and "6 0 NOTIFY" before "6 1 NOTIFY".
Constraints
1 <= intervals.length <= 10thresholds.length == repeatIntervals.length == values.length == intervals.length1 <= intervals[i] <= 10^40 <= repeatIntervals[i] <= 10^6-10^9 <= thresholds[i] <= 10^91 <= values[i].length <= 200-10^9 <= values[i][j] <= 10^9
Similar questions at other companies
- Document Enrichment Stream AggregatorBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Progressive Banking SystemAirbnb · OAAirbnb interview questions
- Cloud Compute Credits SystemCoinbase · OACoinbase interview questions
- Streaming Median of Trade PricesXantium · PHONE SCREEN
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Top K Pins with Insert and QueryPinterest · PHONE SCREENPinterest interview questions
- Finish Time of Fixed-Length CPU TasksGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Worst-Performing Ads by CTR in a Campaign TreeUber · PHONE SCREENUber interview questions
- Camel CardsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Timer Job Scheduler Wake-upsRedNote · PHONE SCREEN