Count the Number of Fair Pairs
A medium-tier problem at 53% community acceptance, tagged with Array, Two Pointers, Binary Search. Reported in interviews at Salesforce and 1 others.
Count the Number of Fair Pairs is a medium-difficulty array problem that shows up in Salesforce and Qualcomm assessments. You're given an array and a range, and you need to count pairs where the sum falls within that range. The naive brute force approach times out immediately, which is exactly the trap candidates walk into. About 53% of submissions pass, so you're not alone if this one stalls you mid-assessment. If you blank on the two-pointer optimization during your live OA, StealthCoder surfaces a working solution invisibly so you move forward without panic.
Companies that ask "Count the Number of Fair Pairs"
Count the Number of Fair Pairs 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 because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround.
Get StealthCoderThe trick is sorting the array, then using two pointers to count valid pairs in O(n log n) time instead of O(n squared). Most candidates start with nested loops and hit the time limit wall. Once sorted, you place one pointer at the start and one at the end, then slide them based on whether the current sum is too small or too large. Binary search is another viable path if you're comfortable with it. The problem tests whether you recognize that a sorted array unlocks a linear pair-counting strategy. When this appears in your assessment, it's typically a signal they want to see optimization thinking, not just correct logic. StealthCoder handles both the sorting insight and the pointer mechanics so you don't lose time debugging a TLE.
Pattern tags
You know the problem.
Make sure you actually pass it.
Count the Number of Fair Pairs 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 because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count the Number of Fair Pairs interview FAQ
Is this problem still asked at big tech companies?+
Yes, Salesforce and Qualcomm both report asking it. It's a solid medium-level assessment problem because it separates candidates who reach for the obvious O(n squared) solution from those who spot the sorted + two-pointer pattern. Not as common as some classics, but frequent enough that you shouldn't skip it.
What's the actual trick here?+
Sorting the array unlocks a two-pointer approach. Once sorted, you iterate with pointers at start and end, shrinking the window based on whether the sum is too small or too large. This collapses O(n squared) brute force into O(n log n). Binary search is also valid but slower in practice.
Why do so many submissions fail?+
The 53% acceptance rate reflects that candidates either miss the optimization entirely and time out, or they implement two-pointer logic but make off-by-one errors in counting. Edge cases around the boundary of the valid range trip people up too.
How does sorting relate to the two-pointer topic?+
They're paired. Two pointers only work cleanly on sorted data because order is predictable. Without sorting, you can't guarantee that moving a pointer narrows or expands the sum range correctly. Sorting is the prerequisite that makes two-pointer counting viable.
Will I have time to code this during a live assessment?+
Yes, if you know the pattern. Sort, then iterate with two pointers while counting valid sums. The logic is about 10-15 lines once you see it. The real bottleneck is recognizing you need to sort first instead of defaulting to nested loops.
Want the actual problem statement? View "Count the Number of Fair Pairs" on LeetCode →