Find K Pairs with Smallest Sums
A medium-tier problem at 41% community acceptance, tagged with Array, Heap (Priority Queue). Reported in interviews at LinkedIn and 0 others.
Find K Pairs with Smallest Sums is a classic heap problem that LinkedIn and other companies use to filter out candidates who conflate "smallest K" with sorting. The acceptance rate sits at 40 percent, which tells you most people either brute-force and time out, or misunderstand the constraint space. You're given two sorted arrays and need to return K pairs with the smallest sums, where each pair is (arr1[i], arr2[j]). The trick isn't to generate all pairs and sort them. If you blank on the heap strategy during your live assessment, StealthCoder runs invisibly and surfaces the working solution in seconds.
Companies that ask "Find K Pairs with Smallest Sums"
Find K Pairs with Smallest Sums 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. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop.
Get StealthCoderThe naive approach kills you: generate all possible pairs (O(m*n)), sort (O(m*n*log(m*n))), return first K. On large arrays, that's TLE. The real pattern is a min-heap seeded with only the most promising pair, (arr1[0], arr2[0]). You pop the smallest, then push its two "neighbors" into the heap: the next pair by advancing the first index, and the next pair by advancing the second index. Track visited pairs to avoid duplicates. This runs in O(K*log(K)) instead of O(m*n*log(m*n)). The mental block most candidates hit: they want to sort both arrays first, or they think they need to examine all combinations. Neither is true. The heap + smart enumeration pattern is the hedge that separates pass from fail. StealthCoder knows this pattern and executes it cleanly when you're stuck live.
Pattern tags
You know the problem.
Make sure you actually pass it.
Find K Pairs with Smallest Sums recycles across companies for a reason. It's medium-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. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find K Pairs with Smallest Sums interview FAQ
Why can't I just generate all pairs, sort them, and return the first K?+
You can, but it's O(m*n*log(m*n)) on two arrays of sizes m and n. If both are 5000 and K is 100, you're doing 25 million comparisons just to get 100 results. Online assessments have tight time limits. The heap strategy is O(K*log(K)) and will pass. Brute force won't.
How do I avoid revisiting the same pair in the heap?+
Track visited pairs using a set of indices tuples. When you pop (arr1[i], arr2[j]) from the heap, only push (arr1[i+1], arr2[j]) and (arr1[i], arr2[j+1]) if you haven't seen them. Without this, you'll add the same pair multiple times and skew your results.
Is this problem still asked at LinkedIn?+
Yes. LinkedIn has reported this problem in recent cycles. At 40 percent acceptance, it's designed to catch candidates who don't know the heap + bounded enumeration pattern. If you hit it live and freeze, that's exactly what StealthCoder hedges against.
What's the relationship between this problem and priority queues?+
The min-heap is the core data structure. It enforces the invariant that you always pop the globally smallest pair next, even though you only maintain K candidates at a time. Without the heap, you'd have to scan all m*n pairs yourself. The heap does that automatically in logarithmic time.
Why does the obvious approach of sorting both arrays first fail?+
Sorting both arrays doesn't help you identify which pairs are smallest without still generating and comparing them. The insight is that since both input arrays are already sorted, the smallest pair must be (arr1[0], arr2[0]). From there, the next smallest pair is always a neighbor of one you've already popped. You don't need a global sort.
Want the actual problem statement? View "Find K Pairs with Smallest Sums" on LeetCode →