Find K-th Smallest Pair Distance
A hard-tier problem at 46% community acceptance, tagged with Array, Two Pointers, Binary Search. Reported in interviews at Pinterest and 1 others.
You're staring at an array and need to find the k-th smallest difference between any two elements. This problem shows up at Pinterest and Flipkart, and the 46% acceptance rate tells you the naive brute-force won't cut it. Most candidates try to generate all pairs and sort them, which works but blows up on large inputs. The real trick is binary search on the answer combined with a two-pointer count. If this problem hits your live assessment and you blank on the binary search angle, StealthCoder surfaces a working solution in seconds, invisible to the proctor.
Companies that ask "Find K-th Smallest Pair Distance"
Find K-th Smallest Pair Distance is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made for the engineer who has done the work but might still blank with a webcam pointed at him.
Get StealthCoderThe trap is thinking you have to find and store every pair distance. Instead, you binary search on the distance value itself, then use two pointers on a sorted array to count how many pairs have a distance less than or equal to your candidate answer. Sort the array first. For each potential distance (your binary search space), slide a right pointer and count valid pairs with a left pointer. When your count reaches k or exceeds it, you've narrowed the answer. The key insight: you don't need the pairs, just the count at each distance threshold. Common pitfall is overcounting or getting the binary search bounds wrong. Two Pointers combined with Binary Search is the only efficient path here. StealthCoder handles the pointer logic and boundary conditions that trip up candidates who haven't practiced this exact pattern.
Pattern tags
You know the problem.
Make sure you actually pass it.
Find K-th Smallest Pair Distance recycles across companies for a reason. It's hard-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made for the engineer who has done the work but might still blank with a webcam pointed at him. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find K-th Smallest Pair Distance interview FAQ
Why does brute force fail here?+
Generating all n(n-1)/2 pairs and sorting takes O(n² log n) time. On large arrays, this times out. Binary search on the distance value plus two-pointer counting runs O(n log n) after the initial sort, a massive difference in practice.
Is this still asked at FAANG-adjacent companies?+
Yes. Pinterest and Flipkart both report this problem. It's a classic hard-tier array problem that tests whether you know binary search on the answer, not just binary search on an input array.
What's the two-pointer trick?+
Once you sort, for a given distance threshold, use left and right pointers. Right pointer moves outward, left pointer counts how many elements are within the distance. This counts all valid pairs at that threshold in O(n) per check, fitting inside your binary search loop.
How do I know my binary search bounds?+
Minimum distance is 0 (or the smallest non-zero difference in the array). Maximum is the difference between the largest and smallest elements. Your binary search tightens the range until you find the exact k-th smallest distance.
What topics do I need?+
Sorting, Binary Search, and Two Pointers are all required. Array manipulation is basic. Most candidates know each topic individually but struggle to see them together. That's where practice on this specific pattern matters most.
Want the actual problem statement? View "Find K-th Smallest Pair Distance" on LeetCode →