Find Shared Motion Periods Across Camera Streams
A camera emits readings (timestamp, intensity) sorted by strictly increasing timestamp. Intensity is between 0 and 1. A reading is active when its intensity is at least a supplied threshold.
Implement both parts.
Part A - Active Periods for One Camera
Consecutive active readings form one closed period whose start and end are the first and last active timestamps. An inactive reading ends the current period. Timestamp gaps alone do not split a period.
active_periods(readings, threshold) -> list[[start, end]]
Example:
readings = [
[1, 0.4], [5, 0.2], [11, 0.9], [15, 0.9], [17, 0.8],
[20, 0.3], [27, 0.9], [31, 1.0], [36, 0.8]
]
threshold = 0.8
result = [[11, 17], [27, 36]]
A single isolated active reading produces [timestamp, timestamp].
Part B - Periods Shared by Every Camera
Given a list of camera streams, return the closed time intervals during which every camera is active. You may reuse Part A for each stream, then intersect the resulting sorted, disjoint interval lists.
shared_active_periods(camera_streams, threshold) -> list[[start, end]]
Example:
camera_streams = [
[[1, 0.9], [4, 0.9], [6, 0.1], [9, 1.0]],
[[2, 0.8], [5, 0.8], [7, 0.2], [9, 0.9]]
]
threshold = 0.8
per-camera periods = [[[1, 4], [9, 9]], [[2, 5], [9, 9]]]
result = [[2, 4], [9, 9]]
Constraints
-
Each stream contains at most 100000 readings.
-
0 <= len(camera_streams) <= 1000
.
-
For no cameras, return an empty list.
-
Periods are closed; an intersection at one timestamp is retained.
-
Inputs must not be mutated.
Hints
-
Part A is one linear scan with an optional open-period start.
-
Intersect two sorted interval lists with two pointers, then fold that operation across cameras.
-
Stop early if an intermediate intersection becomes empty.
Discussion Extensions
-
State complexity in terms of total readings and generated intervals.
-
How would out-of-order readings change the design?
-
How would you process unbounded live streams while bounding memory?