Find common active intervals across cameras
Company: Verkada
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates competency in interval processing, temporal data aggregation, and multi-stream synchronization, within the Coding & Algorithms domain, and requires practical application-level understanding of interval intersection and time-stamped sensor data handling.
Part 1: Compute Active Intervals for One Camera
Constraints
- 0 <= len(readings) <= 200000
- Each reading is [time, motion_level]
- time values are numeric and strictly increasing
- motion_level and T are numeric values
- A reading is active only when motion_level > T (strictly greater, not greater-than-or-equal)
Examples
Input: ([[1, 0.2], [2, 0.7], [3, 0.8], [4, 0.1], [5, 0.9]], 0.5)
Expected Output: [[2, 3], [5, 5]]
Explanation: Readings at times 2 and 3 form one consecutive active run, and time 5 is a single active reading.
Input: ([[1, 0.1], [2, 0.2], [3, 0.5]], 0.5)
Expected Output: []
Explanation: No reading is strictly greater than the threshold.
Input: ([], 1.0)
Expected Output: []
Explanation: Empty input has no active intervals.
Input: ([[10, 1.0]], 1.0)
Expected Output: []
Explanation: The only reading equals the threshold, so it is not active.
Input: ([[0.5, 2.0], [1.0, 1.0], [1.5, 2.1], [2.0, 2.2]], 1.5)
Expected Output: [[0.5, 0.5], [1.5, 2.0]]
Explanation: The first reading is an isolated active interval, and the last two readings form a second active run.
Solution
def solution(readings, T):
intervals = []
start = None
last_active_time = None
for time, motion_level in readings:
if motion_level > T:
if start is None:
start = time
last_active_time = time
else:
if start is not None:
intervals.append([start, last_active_time])
start = None
last_active_time = None
if start is not None:
intervals.append([start, last_active_time])
return intervalsTime complexity: O(n), where n is the number of readings.. Space complexity: O(1) extra space, excluding the output list..
Hints
- Scan the readings once from left to right and track whether you are currently inside an active run.
- When you move from an active reading to an inactive reading, close the current interval.
Part 2: Find Maximal Intervals Where All Cameras Are Simultaneously Active
Constraints
- 1 <= number of cameras <= 10000
- The total number of intervals across all cameras is at most 200000
- For every interval [start, end], start <= end
- Within each camera's list, intervals are sorted by start time and do not overlap
- Time values may be integers or floating-point numbers
Examples
Input: ([[[1, 5], [8, 10]], [[2, 6], [8, 9]], [[0, 3], [4, 9]]],)
Expected Output: [[2, 3], [4, 5], [8, 9]]
Explanation: These are the maximal segments where all three cameras are active together.
Input: ([[[1, 2]], [[3, 4]]],)
Expected Output: []
Explanation: The cameras are never active at the same time.
Input: ([[], [[1, 2], [3, 5]]],)
Expected Output: []
Explanation: If one camera has no active intervals, there can be no common overlap.
Input: ([[[1, 5]], [[5, 7]], [[0, 5]]],)
Expected Output: [[5, 5]]
Explanation: All cameras overlap exactly at time 5.
Input: ([[[2, 4], [6, 6]]],)
Expected Output: [[2, 4], [6, 6]]
Explanation: With only one camera, the common active intervals are just that camera's intervals.
Solution
def solution(intervals_by_camera):
def intersect_two(a, b):
i = 0
j = 0
result = []
while i < len(a) and j < len(b):
start = max(a[i][0], b[j][0])
end = min(a[i][1], b[j][1])
if start <= end:
result.append([start, end])
if a[i][1] < b[j][1]:
i += 1
else:
j += 1
return result
if not intervals_by_camera:
return []
current = [[start, end] for start, end in intervals_by_camera[0]]
for k in range(1, len(intervals_by_camera)):
current = intersect_two(current, intervals_by_camera[k])
if not current:
return []
return currentTime complexity: O(K), where K is the total number of intervals across all cameras.. Space complexity: O(M), where M is the size of the running intersection/result..
Hints
- First solve the easier subproblem: how do you intersect two sorted interval lists using two pointers?
- Then repeatedly intersect the running result with the next camera's interval list.