K-diff Pairs in an Array
A medium-tier problem at 45% community acceptance, tagged with Array, Hash Table, Two Pointers. Reported in interviews at Capital One and 1 others.
K-diff Pairs in an Array is a medium-difficulty problem that tests your ability to find unique pairs with a specific difference in unsorted data. Capital One and TCS have both asked it. The catch: you need to return unique pairs, not all occurrences, and the obvious brute force will TLE or fail on edge cases. A 44% acceptance rate means most candidates either miss the constraint or pick the wrong approach. If you hit this live and freeze, StealthCoder surfaces a working solution in seconds, invisible to the proctor.
Companies that ask "K-diff Pairs in an Array"
K-diff Pairs in an Array 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 a senior engineer who knows the OA is theater. This is the script.
Get StealthCoderThe trick is recognizing that you can't just iterate and count. You need to either use a hash table to track seen numbers and avoid duplicates, or sort and use two pointers to skip redundant pairs. The first approach feels cleaner but requires careful logic to skip duplicate values and avoid counting the same pair twice. The sorting approach is safer because two pointers naturally skip duplicates if you advance correctly. Common fail points: forgetting to handle k=0 (pairs like [1, 1]), miscounting unique pairs instead of occurrences, or using a set that blocks valid pairs. If you've drilled two-pointer problems, you'll recognize the pattern fast. If not, StealthCoder hedges the gap between what you studied and what the assessment throws at you.
Pattern tags
You know the problem.
Make sure you actually pass it.
K-diff Pairs in an Array 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 a senior engineer who knows the OA is theater. This is the script. Works on HackerRank, CodeSignal, CoderPad, and Karat.
K-diff Pairs in an Array interview FAQ
Why is acceptance so low at 44%?+
Most failures come from either counting all pair occurrences instead of unique pairs, or not handling the k=0 edge case where you're pairing identical numbers. The problem statement is tighter than it appears at first read.
Should I sort or use a hash table?+
Both work. Sorting plus two pointers is more robust for avoiding duplicate logic errors. Hash table is faster in theory but requires more careful bookkeeping to skip duplicates. For a live assessment, sort-based is lower risk.
How does this relate to the two-pointer topic?+
If you sort first, the two-pointer technique handles the search and duplicate-skipping elegantly. You advance both pointers and check diff at each step, skipping over matching values automatically.
Is this still asked at Capital One or TCS?+
Yes, it's in both companies' reported problem sets. It's common enough that you should be prepared, but not so frequent that it'll always appear. It's a solid hedge problem to drill before an OA.
What's the main pitfall in the hash table approach?+
Filtering unique pairs after finding them. Many candidates iterate through the hash and double-count symmetrically. You need to track visited pairs or use careful pointer logic to avoid that trap.
Want the actual problem statement? View "K-diff Pairs in an Array" on LeetCode →