Requirements
- Input: sorted
(timestamp, motion_intensity)pairs and a threshold between 0 and 1. - Return inclusive start/end timestamp pairs for periods at or above the threshold.
- Extend the function to multiple camera streams and return intervals when ALL cameras meet the threshold simultaneously.
- Follow-up: discuss bottlenecks when the number of cameras becomes large.
Examples
Input [[1,0.4],[5,0.2],[11,0.9],[15,0.9],[17,0.8],[25,0.5],[27,0.8],[36,0.9]], threshold 0.8, returns [[11,17],[27,36]].
Notes
- The main variant includes readings equal to the threshold.
- Minority variant: use strictly greater than the threshold; clarify before coding.
One candidate lost unsaved edits after refreshing the coding environment between parts.
Solution approach
Scan each sorted stream once. Open a run on the first qualifying sample and remember the latest qualifying timestamp. A below-threshold reading closes the run at that remembered timestamp, not at the failing reading; flush an open run at end of input. This takes O(N) time and O(1) working space beyond the output for N readings.
For two cameras, intersect their sorted closed-interval lists with two pointers: use the larger start and smaller end, retaining the result when start <= end, then advance the interval with the earlier end (both on a tie). This takes O(P+Q) time for P and Q intervals. Repeatedly intersect with the remaining cameras to implement ALL-camera semantics; an empty intersection ends the calculation. For many cameras, measure intermediate-list sizes and consider a sweep over one active interval per camera. Do not interpret missing samples as zero intensity without an agreed sampling rule.
Preparation
- In 15 minutes, implement the single-camera scan and test an empty stream, equality at the threshold, a single qualifying sample, and a run that reaches the end.
- In 20 minutes, implement closed-interval intersection, then fold it across three camera streams; check a shared endpoint and a camera with no qualifying interval.

