Longest Latency Window Where Max Minus Min Stays Within Limit
Last updated: November 29, 2026
Quick Overview
Given request latencies in time order and a limit, return the length of the longest contiguous window whose maximum minus minimum latency is at most the limit.
Meta
November 29, 20260
0
0 solved
Given request latencies in time order and a limit, return the length of the longest contiguous window whose maximum minus minimum latency is at most the limit.
Asked at Meta in technical screens with a production-monitoring framing: finding the longest stable stretch of a service's p50 latency. The interviewer wants a sliding window that tracks both extremes in O(1) amortized with two monotonic deques rather than a sorted structure.
What the Interviewer Expects
- Use a two-pointer sliding window that only grows or shifts
- Maintain a decreasing deque for the max and an increasing deque for the min
- Shrink from the left when max - min exceeds the limit
- Evict deque fronts that leave the window
- Compare with a sorted-container O(n log n) alternative
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return the start and end index of the window as well.
- Count all windows that satisfy the limit.
- How would you do this over a stream with a fixed memory budget?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list lat of integers and integer limit >= 0. Output: the maximum j - i + 1 such that max(lat[i..j]) - min(lat[i..j]) <= limit. Non-empty input always has an answer of at least 1....
High-Level Design
Expand the right edge one step at a time. Keep maxq, indices with strictly decreasing latency, and minq, indices with strictly increasing latency; their fronts are the window max and min. When add...