The Question
Coding

Health Anomaly Detection

Given a stream of health metric readings and a threshold, identify all contiguous time windows where the metric continuously exceeds the threshold for a specified minimum duration.
Python
Sliding Window
Two Pointers
Questions & Insights

Thinking Process

Identify Data Patterns: Recognize that health data is essentially a time-series stream. Detecting "abnormal" health events requires identifying contiguous segments where values exceed a safety threshold for a specific duration.
Two-Pointer/Sliding Window Approach: Use a single-pass linear scan to track the start and end of high-rate periods. This avoids the O(N^2) complexity of checking all possible sub-segments.
State Management: Maintain a start pointer initialized to -1. When the threshold is crossed, lock the start index. When the rate returns to normal, calculate the duration (current_index - start).
Edge Case Handling: Ensure that if the data stream ends while the heart rate is still high, the final segment is captured and validated against the duration constraint.
Implementation Breakdown

Problem Set

Functional Requirements:
Input: An array of integers representing heart rate readings.
Input: A threshold value (e.g., 100 BPM).
Input: A min_duration (e.g., 3 consecutive readings).
Output: A list of tuples (start_index, end_index) representing periods where heart rate was strictly above the threshold for at least the minimum duration.
Constraints:
Time Complexity must be O(N) for real-time processing capability.
Space Complexity should be O(1) (excluding the result list).
The input array can be empty or have lengths smaller than min_duration.

Approach

Logic: Linear scan with state-tracking of the "High Heart Rate" state.
Algorithm: Two-Pointer / Linear Scanning.
Data Structure: Array/List.
Complexity:
Time: O(N) where N is the number of readings.
Space: O(K) where K is the number of abnormal periods detected (output space).

Implementation