Count Request Windows Whose Median Latency Rank Equals K
Last updated: November 29, 2026
Quick Overview
Given a permutation of the latency ranks 1..n for consecutive requests and a target rank k, count the contiguous windows whose median is k, where the median of an even-length window is the lower of the two middle values.
0
0
0 solved
Given a permutation of the latency ranks 1..n for consecutive requests and a target rank k, count the contiguous windows whose median is k, where the median of an even-length window is the lower of the two middle values.
A Google onsite problem framed around latency analysis, where ranks are distinct so the median is well defined. The interviewer is looking for the reduction to a +1/-1 balance around the position of k and a hash map of left-side balances, rather than sorting every window.
What the Interviewer Expects
- Observes every qualifying window must contain k
- Maps values above k to +1 and below k to -1
- Derives that the balance must be 0 (odd length) or 1 (even length)
- Counts left-side balances in a hash map, then scans right
- Achieves O(n) time
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
- What if values are not distinct?
- Count windows whose median is at least k.
- Return the longest window with median k.
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
ranks, a permutation of 1..n, andkin 1..n. - Output: number of subarrays whose median (lower middle for even lengths) equals k.
- n up to 10^5....
High-Level Design
Since values are distinct, a window has median k exactly when it contains k and, among the other elements, the number greater than k (G) and the number smaller than k (S) satisfy G - S = 0 (odd length...