Detect Trigger and Resolve Events
Company: Stripe
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates handling of time-ordered log streams, rolling-window aggregation, and state transition detection for keyed pairs, testing competence in algorithms, stateful stream processing, and temporal data handling.
Constraints
- 0 <= len(logs) <= 200000
- 0 <= timestamp <= 10^9, and logs are sorted by non-decreasing timestamp
- 1 <= window_size <= 10^9
- 1 <= threshold <= 10^12
- 1 <= count <= 10^9
- merchant_id and status_code can be used as dictionary keys
Examples
Input: ([(1, 'm1', 500, 2), (2, 'm1', 500, 2), (5, 'm1', 500, 1), (6, 'm1', 500, 1)], 3, 4)
Expected Output: [(2, 'm1', 500, 'TRIGGER'), (5, 'm1', 500, 'RESOLVE')]
Explanation: At time 2, the rolling count for ('m1', 500) becomes 4, crossing the threshold and triggering an alert. At time 5, the earlier counts have expired from the 3-second window, so the rolling count drops to 1 and emits RESOLVE.
Input: ([(1, 'A', 404, 2), (1, 'A', 404, 1), (2, 'B', 500, 3), (3, 'A', 404, 1), (4, 'B', 500, 1)], 3, 3)
Expected Output: [(1, 'A', 404, 'TRIGGER'), (2, 'B', 500, 'TRIGGER')]
Explanation: The second log at timestamp 1 brings ('A', 404) to a rolling count of 3, so it triggers. Pair ('B', 500) reaches 3 at time 2 and also triggers. Later logs keep both pairs above threshold, so no duplicate TRIGGER events are emitted.
Hints
- Treat each (merchant_id, status_code) pair as its own independent stream of records.
- For each stream, use a deque to remove expired records from the left and keep a running sum of counts in the current window.