Find the Kth Smallest Latency Gap Among All Server Pairs
Last updated: November 29, 2026
Quick Overview
Given probe latencies for n servers in a region, consider the absolute difference for every pair of servers. Return the k-th smallest of these n(n-1)/2 differences without listing them all.
Microsoft
November 29, 20260
0
0 solved
Given probe latencies for n servers in a region, consider the absolute difference for every pair of servers. Return the k-th smallest of these n(n-1)/2 differences without listing them all.
A harder binary search on answer problem used in Microsoft onsite loops. The interviewer looks for the jump from 'k-th smallest of a huge implicit set' to 'count how many are at most d', and for an O(n) two-pointer count.
What the Interviewer Expects
- Avoid generating O(n^2) pairs
- Binary search on the gap value d
- Count pairs with gap at most d using a sliding window on sorted data
- Find the smallest d whose count reaches k
- Argue that the result is an actual pair difference
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 pair, not just the gap.
- Values are floating point. How do you adapt the search?
- Could a heap solve it, and when would that be better?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
latencies: integers, n >= 2.1 <= k <= n(n-1)/2.- Output: the k-th smallest |a[i] - a[j]| for i < j....
High-Level Design
Sort the values. For a candidate gap d, pairs_within(d) counts pairs whose difference is at most d: as right moves forward, advance left until a[right] - a[left] <= d; then every index from `l...