Sliding-window analysis of server logs
viaGlassdoor
Problem Given a stream of timestamped server-log / time-series entries, find the window (contiguous subarray) that satisfies a given constraint — for example, the longest window whose request rate stays under a threshold, or the shortest window whose total exceeds a target.
Input / Output
- Input: an array of entries (timestamp and/or value), and a constraint parameter (threshold, target sum, or distinct-count limit).
- Output: the qualifying window — its length and/or its boundary indices.
Constraints
- Up to ~1e6 entries; aim for a single pass.
- Entries arrive in non-decreasing timestamp order.
Example
- values = [2, 1, 5, 1, 3, 2], target sum ≥ 8 → shortest window = [5, 1, 3] (length 3).
Expected approach
- Two-pointer / sliding window: expand
right, shrinkleftwhile the window violates (or to tighten) the constraint, tracking the best window seen. - For "rate under threshold" use timestamps to bound the window width; a monotonic deque handles max/min-in-window variants.
- Time O(n), space O(1) (or O(k) for distinct-count variants).
asked …