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
Coding & Algorithms
Software Engineer
Microsoft
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

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
binary search on answer
two pointers
sorting
counting
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported