EASYasked at 3 companies

Count Number of Pairs With Absolute Difference K

A easy-tier problem at 85% community acceptance, tagged with Array, Hash Table, Counting. Reported in interviews at Expedia and 2 others.

Founder's read

You've seen this one in reports from Expedia, Oracle, and Goldman Sachs. Count Number of Pairs With Absolute Difference K is categorized easy, but the acceptance rate sits at 85%, which means a lot of people miss the optimization layer or get tangled in edge cases. The problem asks you to count pairs where the absolute difference equals a fixed value K. It's a warm-up on the surface, but it's testing whether you reach for the right data structure and avoid O(n²) when the interviewer is watching.

Companies asking
3
Difficulty
EASY
Acceptance
85%

Companies that ask "Count Number of Pairs With Absolute Difference K"

If this hits your live OA

Count Number of Pairs With Absolute Difference K 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 engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage.

Get StealthCoder
What this means

The naive approach is nested loops: compare every element to every other, count matches. It works, but screams 'I didn't think hard.' The pattern click is recognizing that for each number x, you only care if x+K or x-K exists in the array. Hash table (or set) makes this O(n) with one pass to build the map, one pass to count. The trap: duplicates. If the array is [1, 1, 1] and K=0, you're counting all three ones paired with each other, not just unique pairs. Read the problem statement carefully. StealthCoder surfaces the hash-table pattern in seconds if you blank on the approach during your assessment, so you're never stuck choosing between brute force and time limit.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Count Number of Pairs With Absolute Difference K recycles across companies for a reason. It's easy-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 engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Number of Pairs With Absolute Difference K interview FAQ

Is this actually easy or does the difficulty mislead?+

It's genuinely easy in terms of algorithmic complexity, but the 85% acceptance suggests people either brute force it or trip on the duplicate/pair-counting logic. If you spot the hash table insight, you nail it in under three minutes.

Do I need to worry about negative numbers or zero?+

The problem doesn't restrict the input range in typical constraints. Negative numbers don't change the logic, absolute difference still works. K itself is always non-negative in standard versions. Check the exact problem statement, but don't overthink it.

Why is this asked at places like Goldman Sachs if it's easy?+

Large finance and tech companies use easy problems early in the loop to screen for baseline competency and code cleanliness. It's a filter for people who panic or write sloppy code, not a signal of role complexity. Pass it cleanly and move forward.

What's the pair-counting trick when there are duplicates?+

If you have multiple copies of the same number and K=0, you're counting combinations, not unique pairs. Use the formula: if x appears n times and K=0, contribute n*(n-1)/2 pairs. Standard hash table iteration handles it cleanly.

Can I use sorting instead of a hash table?+

Yes. Sort the array, then use two pointers. O(n log n) instead of O(n), but it sidesteps hash table edge cases and is easier to explain. Both are accepted. Pick whichever feels more natural in the moment.

Want the actual problem statement? View "Count Number of Pairs With Absolute Difference K" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.