Find and Merge Camera Alert Intervals
Company: Verkada
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of time-series processing, interval detection, and interval-merging algorithms, including handling thresholds, touching intervals, and open-ended alerts.
Part 1: Single Camera Alert Intervals
Constraints
- `0 <= len(readings) <= 200000`
- `-10^9 <= timestamp, value, threshold <= 10^9`
- Timestamps are strictly increasing
- An alert reading is defined by `value > threshold`
Examples
Input: ([(1, 1), (2, 6), (4, 7), (5, 3), (8, 9), (10, 2)], 5)
Expected Output: [[2, 4], [8, 8]]
Explanation: The camera is above the threshold at timestamps 2 and 4, then again at timestamp 8.
Input: ([(1, 7), (3, 8), (6, 4), (9, 5), (10, 6)], 5)
Expected Output: [[1, 3], [10, 10]]
Explanation: The first interval starts immediately at timestamp 1. The value at timestamp 9 equals the threshold, so it is not alert. A new interval starts at 10 and is closed at the end.
Input: ([(1, 1), (2, 2), (3, 5)], 5)
Expected Output: []
Explanation: No value is strictly greater than the threshold.
Input: ([], 0)
Expected Output: []
Explanation: Empty input produces no intervals.
Input: ([(4, 10)], 3)
Expected Output: [[4, 4]]
Explanation: A single reading above the threshold forms a one-point interval.
Hints
- Scan the readings once from left to right while tracking whether you are currently inside an alert interval.
- Keep the most recent timestamp that was still above the threshold so you know where to close an interval when the alert stops.
Part 2: Merge Alert Intervals From Multiple Cameras
Constraints
- `0 <= number of cameras <= 100000`
- `0 <= total number of intervals across all cameras <= 200000`
- `-10^9 <= start <= end <= 10^9`
- Within each camera, intervals are sorted by start time and do not overlap
- Touching intervals must be merged
Examples
Input: ([[[1, 3], [8, 10]], [[2, 5]], [[10, 12], [15, 18]]],)
Expected Output: [[1, 5], [8, 12], [15, 18]]
Explanation: Intervals from different cameras overlap or touch, so they are merged.
Input: ([[[1, 1]], [[1, 3]], [[3, 5]]],)
Expected Output: [[1, 5]]
Explanation: All intervals overlap or touch at the endpoints, so they become one interval.
Input: ([[], [[2, 4]], [[6, 7]]],)
Expected Output: [[2, 4], [6, 7]]
Explanation: One camera has no intervals, and the remaining intervals are disjoint.
Input: ([[], []],)
Expected Output: []
Explanation: No camera reports any alert interval.
Input: ([[[4, 9]]],)
Expected Output: [[4, 9]]
Explanation: A single interval is already fully merged.
Hints
- First collect all intervals into one list, then sort them by start time.
- While merging, if the next interval starts at or before the current merged interval ends, they should be combined.