Implement alert queries and spike detection
LinkedInMachine Learning Engineer · OnsiteLast updated
This problem tests your ability to process a stream of events with time-based constraints, requiring an efficient sliding-window approach and the use of monotonic data structures for spike detection.
Problem Statement
You are building an alerting system that monitors a high-throughput stream of numerical metrics. You need to implement a class AlertSystem that tracks the last k seconds of data and triggers an alert if the current metric is significantly higher than previous values.
Define the class AlertSystem with the following methods:
record(timestamp, value): Records a metricvalueat a specific integertimestamp(in seconds).get_spike_count(window_size): Returns the number of events within the lastwindow_sizeseconds that are strictly smaller than the current (most recent) metric.
Constraints
- Timestamps arrive in non-decreasing order.
window_sizeis an integer representing the lookback period in seconds.- The system must handle frequent
recordcalls efficiently.
Example
# Timeline: (t=1, v=10), (t=2, v=5), (t=3, v=12), (t=4, v=8), (t=5, v=15)
sys = AlertSystem()
sys.record(1, 10)
sys.record(2, 5)
sys.record(3, 12)
sys.record(4, 8)
sys.record(5, 15)
# For get_spike_count(3):
# Look at events in the last 3 seconds (t=3, 4, 5).
# Values: [12, 8, 15].
# 12 < 15 and 8 < 15. Count = 2.
print(sys.get_spike_count(3)) # Output: 2
Edge Cases
window_sizelarger than the total duration of recorded events.- Multiple events occurring at the same timestamp.
- Empty history.
Hints
Hint 1
Use a Deque or a list of tuples to store events in a way that allows efficient removal of outdated entries.
Hint 2
Since you only care about events within the last 'window_size', consider using a two-pointer approach or binary search on the recorded history to isolate the relevant time slice.
Hint 3
If the number of calls to 'record' is massive, consider how you might maintain a sorted window or an auxiliary structure to avoid re-scanning the history for every 'get_spike_count' call.
Follow-up questions
- How would you modify this solution if the timestamps arrived out-of-order?
- What data structure would you use if we needed to calculate the 'moving average' of the window in O(1) time?
- How would you scale this system to handle millions of events per second across multiple distributed nodes?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.