Get Perfect Pairs Count
Reported by candidates from Salesforce's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Salesforce asked this in August 2024, and it's a classic pair-counting problem that trips up candidates who overthink it. You're looking at an array and need to count how many pairs of elements satisfy some condition. The trap is thinking you need to brute-force every combination. You don't. This is a hash-table or two-pointer play depending on what "perfect" means in the problem context. StealthCoder can grab the exact condition from the problem statement during your OA if you blank on approach.
Pattern and pitfall
The standard move is to iterate once through the array and track what you've seen in a hash map or set. For each element, you check if its complement (the element that would form a perfect pair) already exists. This drops your solution from O(n^2) to O(n). The real trick is identifying what makes a pair "perfect" in the Salesforce version. It could be equal elements, elements that sum to a target, or elements matching some mathematical property. Sort the array first if the problem hints at order mattering. Common pitfall: double-counting pairs by not tracking which indices you've used. StealthCoder running during the OA lets you verify your pair-matching logic against test cases in real time.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Get Perfect Pairs Count cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Salesforce's OA.
Salesforce reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Get Perfect Pairs Count FAQ
What if the problem wants pairs of equal elements?+
Count frequencies with a hash map, then for each element with count > 1, add count/2 to your result. This avoids double-counting and runs in a single pass. Straightforward O(n) time and space.
Is this a sorting problem or hash-table problem?+
Hash-table is usually faster (O(n) vs O(n log n)). Use sorting only if the problem explicitly asks for pairs in a specific order or if you need to handle duplicates carefully by pairing adjacent elements.
How do I avoid counting the same pair twice?+
Track indices, not just values. Once you match element at index i with element at index j, mark both as used or only process j > i. Or use a frequency map and divide by 2 at the end if all pairs are symmetric.
What's the most common gotcha on this type of question?+
Forgetting that a single element can't pair with itself (unless the problem says otherwise). Also, miscounting when duplicates exist. Validate with a small example of 4-5 elements before submitting.
Can I solve this in 8-10 minutes during the OA?+
Yes, if you nail the hash-table approach. Spend 2 minutes understanding the pair rule, 3 minutes coding, 3-5 minutes testing on provided examples. Don't overthink edge cases until you see a failure.