Number of Good Pairs
A easy-tier problem at 90% community acceptance, tagged with Array, Hash Table, Math. Reported in interviews at tcs and 0 others.
Number of Good Pairs is an easy problem with a deceptively high acceptance rate of 89.5%, but that's because most candidates who reach it have already warmed up. You might see it early in a TCS assessment or as a warm-up round. The problem tests whether you can count pairs efficiently without brute force. The naive solution works for small inputs but fails the moment constraints tighten. Even easy problems hide gotchas. If you blank on the counting math during the live OA, StealthCoder surfaces a working solution in seconds, invisible to the proctor.
Companies that ask "Number of Good Pairs"
Number of Good 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 by a senior engineer who knows the OA is theater. This is the script.
Get StealthCoderA good pair is two indices where i < j and nums[i] == nums[j]. Most candidates write nested loops first. That's O(n^2) and it passes most test cases. The trick is recognizing that you don't need to iterate pairs. If you have n occurrences of the same number, the number of pairs is n * (n - 1) / 2. Hash Table or Array topics signal this counting insight. Count frequency of each number, then sum the formula across all frequencies. That's O(n) time and O(n) space. Math topic is included because the combinatorial formula matters more than the iteration. Candidates who jump straight to nested loops often miss the optimization window in live coding. StealthCoder is your hedge if the O(n^2) approach times out and you need the O(n) counting method under pressure.
Pattern tags
You know the problem.
Make sure you actually pass it.
Number of Good Pairs 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 a senior engineer who knows the OA is theater. This is the script. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Number of Good Pairs interview FAQ
Is Number of Good Pairs actually asked in interviews.+
Yes. TCS has reported it. It's typically an easy warm-up or early-round problem, not a decision-maker, but it can appear in online assessments. Treat it as a real problem, not a gimme.
What's the core trick everyone misses.+
Candidates iterate pairs (O(n^2)). The trick is combinatorics: count each number's frequency, then use n * (n - 1) / 2 to get pairs without iterating them. One pass through the array, one pass through counts. O(n) time wins.
Which topic matters most: Array, Hash Table, or Math.+
All three signal the solution. Array is the input format. Hash Table is the data structure (frequency map). Math is the combinatorial formula. You need all three in your solution. The Math topic is the differentiator.
Can I pass with the nested-loop O(n^2) solution.+
Probably yes, depending on constraints. But in a live OA under time pressure, your nested loops might time out on large inputs or run out of time to debug. The O(n) counting solution is safer and faster to code once you see the pattern.
What if I forget the combinatorial formula during the OA.+
The formula is n * (n - 1) / 2 for n occurrences of a value. Derive it logically: n choices for first index, n - 1 for second, divide by 2 to avoid double-counting. Or code a loop inside. Either works, but the formula is faster under time pressure.
Want the actual problem statement? View "Number of Good Pairs" on LeetCode →