Count Equal Reversal-Difference Pairs
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The whole problem lives or dies on a hash map, and you've got to see that before you start writing nested loops. ZipRecruiter reported this one in February 2022: count pairs (i,j) with i <= j where numbers[i] + reverse(numbers[j]) equals numbers[j] + reverse(numbers[i]). With up to 100000 elements, brute force is dead on arrival. The trick is a rearrangement that turns a pair condition into a single-value key. If you've got the OA in a day or two, this is a pattern you can spot fast. StealthCoder is the safety net if your mind goes blank mid-assessment.
The problem
Count index pairs (i,j) with i <= j such that numbers[i] + reverse(numbers[j]) == numbers[j] + reverse(numbers[i]). Decimal reversal discards leading zeros. Function countReversalPairs(numbers: int[]) → long Examples Example 1 numbers = [42,24] return = 2 Both self-pairs and the cross pair qualify because each difference is 18 or -18? The cross equation is equal only for matching differences, so only the two self-pairs qualify. Example 2 numbers = [10,1] return = 2 The values have different reversal differences, so only their two self-pairs qualify. Constraints 0 <= numbers.length <= 100000 0 <= numbers[i] <= 1000000000
Reported by candidates. Source: FastPrep
Pattern and pitfall
Move terms around. The equation numbers[i] + reverse(numbers[j]) == numbers[j] + reverse(numbers[i]) becomes numbers[i] - reverse(numbers[i]) == numbers[j] - reverse(numbers[j]). So each number gets a key, value minus its reverse, and you count pairs with equal keys. Keep a hash map from key to frequency. For each element, add the current count of its key to the answer, then increment, and include i == j by adding the self-pair too. Easiest is to increment first, then add the count. Pitfalls: the answer needs a 64-bit integer, since 100000 equal keys gives about 5 billion pairs. Reverse must drop leading zeros, which integer reversal does naturally. Empty input returns 0. Note the example 1 explanation is muddled. For [42,24] the keys are 18 and -18, so only self-pairs count. If you blank on the algebra live, StealthCoder is the hedge that surfaces the key idea.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Count Equal Reversal-Difference 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as count nice pairs in an array. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Equal Reversal-Difference Pairs FAQ
What's the trick in this ZipRecruiter problem?+
Rearrange the equation so each side depends on one index. You get value minus reverse(value) equal for both numbers. Then it's a frequency count on that key. Pairs with equal keys qualify, and self-pairs always qualify since the key matches itself.
Why does the return type need to be long?+
If all 100000 numbers share the same key, pairs with i <= j total n(n+1)/2, roughly 5 billion. That overflows a 32-bit int. Use a 64-bit type for the running answer. The map counts themselves fit in a normal int.
How do I handle i == j pairs?+
Every element pairs with itself, since the key equals itself. Easiest approach: for each number, increment its key's count in the map first, then add that new count to the answer. That counts the self-pair plus all earlier matches in one step.
How hard is this really?+
Easy to medium once you see the algebra. The code is about ten lines. The difficulty is noticing the rearrangement instead of testing every pair. Once you have the key, it's just a hash map and a digit-reversal helper.
How do I prepare in 48 hours?+
Practice the pattern of turning a pairwise equation into a per-element key, then counting with a hash map. Write the reverse helper cleanly, handling zero and trailing zeros. Test on [42,24], [10,1], an empty array, and a large all-equal case for overflow.