Num of Good Pairs
Reported by candidates from Commvault's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Commvault hit you with this one in August 2024, and it's a trap if you overthink it. You need to count pairs of indices where the elements at those indices are equal. The instinct is to loop and compare, which works, but the real move is recognizing this as a counting problem that lets you avoid redundant work. If you blank on the elegant approach during the OA, StealthCoder will surface the pattern so you can code confidently instead of guessing.
Pattern and pitfall
The naive solution is O(n-squared): iterate every pair, check if nums[i] == nums[j] where i < j. It works and passes small inputs, but it's clunky. The smart approach uses a hash map to track frequencies as you iterate once through the array. For each element, the number of good pairs it forms with previous elements is exactly its count so far. This transforms the problem into O(n) time and O(n) space. The pattern is counting with a hash table, not brute force. It's the kind of problem where the interviewer is checking if you spot the optimization, not whether you can nest loops. StealthCoder backs you up if you freeze on the accounting logic.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Num of Good Pairs 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 by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as number of good pairs. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Commvault's OA.
Commvault reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Num of Good Pairs FAQ
Is this just brute force or is there a trick?+
There's a trick. Brute force O(n-squared) passes, but the intended path is O(n) using a frequency map. For each element, count how many times you've seen it before. That count is the number of pairs it forms. Add to a running total.
What's the gotcha with off-by-one errors here?+
You need i < j, so the pair is ordered. If you're building a map and processing left to right, you only count prior occurrences of the current element. That constraint is baked in naturally. Don't double-count by mixing directions.
How do I code this in under 5 minutes?+
Declare a hash map and a result counter. Loop through the array once. For each element, add the map's count for that element to result, then increment the map entry. Return result. Three lines of logic, no nested loops.
What if elements are negative or very large?+
Hash maps handle any comparable value. Negative numbers, large integers, even floats if the language allows. The algorithm doesn't care. The constraint is usually array length and element range, not the values themselves.
Is this still a common OA problem in 2024?+
Yes. It's a canonical counting problem that tests whether you reach for a hash table before nested loops. Commvault asked it in August, and companies still use it to filter candidates who think linearly about optimization.