Kth Smallest Latency Gap Among All Pairs of Edge Servers
Last updated: November 29, 2026
Quick Overview
Given the measured round-trip latency of each of n edge servers to a probe, return the k-th smallest absolute latency difference among all n(n-1)/2 server pairs.
ByteDance
November 29, 20260
0
0 solved
Given the measured round-trip latency of each of n edge servers to a probe, return the k-th smallest absolute latency difference among all n(n-1)/2 server pairs.
ByteDance's CDN team groups edge nodes with similar latency profiles, and finding a threshold for the k-th closest pair is a classic Hard in the style of their onsites. The interviewer checks whether you binary search on the gap value and count pairs with a sliding window instead of enumerating all pairs.
What the Interviewer Expects
- Note that enumerating n^2 pairs is too slow for n = 10^5
- Sort latencies first
- Binary search on the gap value d
- Count pairs with gap <= d in O(n) using two pointers
- Find the smallest d with count >= k and state O(n log n + n log W)
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 k-th pair itself, not just the gap.
- Latencies are multi-dimensional vectors: what changes?
- Answer many k values at once efficiently.
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 of n >= 2 integer latencies and 1 <= k <= n(n-1)/2. Output: the k-th smallest value of |a[i] - a[j]| over i < j, counting duplicates. With n around 10^5 there are about 5 * 10^9 pairs, so ...
High-Level Design
After sorting, the gap function is monotonic: the number of pairs with gap at most d grows with d. Binary search for the smallest d where that count reaches k. For a fixed d, slide a left pointer so a...