Find motion intervals above threshold
Problem: Find Motion Intervals Above Threshold
You are tasked with processing data from a single security camera to identify periods of significant motion.
Input
You will be provided with:
- A threshold value
T(a real number). - A vector (array) of time-stamped motion readings. Each reading is a pair
[time, motion_level], where:timeis a strictly increasing integer timestamp, measured in seconds.motion_levelis a real number representing the intensity of motion detected at thattime.
Definition of "Active"
A camera reading is considered "active" whenever its motion_level is strictly greater than the given threshold T (motion_level > T).
Assumptions
- The input vector of readings is guaranteed to be sorted by
timein strictly increasing order. - If two active readings are adjacent in the input array (meaning there are no inactive readings between them), they are considered part of the same continuous active interval, even if there is a time gap between their respective timestamps.
- If a reading is inactive (
motion_level <= T), it does not belong to any active interval.
Objective
Your goal is to return a list of active time intervals, each represented as [start_time, end_time]. These intervals must cover all maximal contiguous segments of readings where motion_level > T. The start_time of an interval should be the timestamp of the first active reading in that segment, and the end_time should be the timestamp of the last active reading in that segment.
Example
- Input:
readings = [[1, 0.4], [5, 0.2], [11, 0.9], [15, 0.9], [17, 0.8], [25, 0.5], [27, 0.8], [36, 0.9]]T = 0.8
- Output:
[[11, 17], [27, 36]]
Explanation of Example
- Readings at times 11, 15, and 17 have
motion_level > 0.8. These form one continuous active interval, starting attime = 11and ending attime = 17. - Readings at times 27 and 36 have
motion_level > 0.8. These form another continuous active interval, starting attime = 27and ending attime = 36. - All other readings have
motion_level <= 0.8and are therefore ignored.
Implement a function that takes the readings and threshold T as input and returns the list of such intervals.
AI interview prep
Core dilemma
Efficiently identifying and consolidating maximal contiguous active segments from a sequential data stream, ensuring correct boundary conditions and minimal state management.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Find motion intervals above threshold
Problem: Find Motion Intervals Above Threshold
You are tasked with processing data from a single security camera to identify periods of significant motion.
Input
You will be provided with:
- A threshold value
T(a real number). - A vector (array) of time-stamped motion readings. Each reading is a pair
[time, motion_level], where:timeis a strictly increasing integer timestamp, measured in seconds.motion_levelis a real number representing the intensity of motion detected at thattime.
Definition of "Active"
A camera reading is considered "active" whenever its motion_level is strictly greater than the given threshold T (motion_level > T).
Assumptions
- The input vector of readings is guaranteed to be sorted by
timein strictly increasing order. - If two active readings are adjacent in the input array (meaning there are no inactive readings between them), they are considered part of the same continuous active interval, even if there is a time gap between their respective timestamps.
- If a reading is inactive (
motion_level <= T), it does not belong to any active interval.
Objective
Your goal is to return a list of active time intervals, each represented as [start_time, end_time]. These intervals must cover all maximal contiguous segments of readings where motion_level > T. The start_time of an interval should be the timestamp of the first active reading in that segment, and the end_time should be the timestamp of the last active reading in that segment.
Example
- Input:
readings = [[1, 0.4], [5, 0.2], [11, 0.9], [15, 0.9], [17, 0.8], [25, 0.5], [27, 0.8], [36, 0.9]]T = 0.8
- Output:
[[11, 17], [27, 36]]
Explanation of Example
- Readings at times 11, 15, and 17 have
motion_level > 0.8. These form one continuous active interval, starting attime = 11and ending attime = 17. - Readings at times 27 and 36 have
motion_level > 0.8. These form another continuous active interval, starting attime = 27and ending attime = 36. - All other readings have
motion_level <= 0.8and are therefore ignored.
Implement a function that takes the readings and threshold T as input and returns the list of such intervals.