Count Reverse-Digit Pairs
Reported by candidates from Visa's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Visa reported this one in August 2026, and the detail that trips people is right in the statement: flip(2050) is 502, so trailing zeros vanish but inner zeros stay. The problem asks you to count index pairs (i, j) with i <= j where nums[i] + flip(nums[j]) = nums[j] + flip(nums[i]). Self-pairs count, and the answer can blow past 32 bits. The hinted pattern is two-pointers, but the real move is algebra plus counting. If you blank during the live assessment, StealthCoder runs invisibly on your desktop and gives you the approach in real time.
The problem
You are given an array of nonnegative integers nums. Define flip(x) by reversing the decimal digits of x and interpreting the reversed digits as an integer. Zeros at the beginning of the reversed representation are discarded. Zeros between other digits are preserved. flip(0) = 0. For example, flip(800) = 8, flip(321) = 123, and flip(2050) = 502. Return the number of index pairs (i, j) that satisfy both of these conditions: 0 <= i <= j < nums.length. nums[i] + flip(nums[j]) = nums[j] + flip(nums[i]). Count pairs of indices, even when several array elements have the same value. Each self-pair (i, i) is included. Return the exact count, without applying a modulo operation; the answer may exceed the range of a signed 32-bit integer. Function countReverseDigitPairs(nums: int[]) → long Examples Example 1 nums = [42,11,1,97] return = 6 All four self-pairs are valid. The two other valid pairs are (0, 3) and (1, 2): 42 + flip(97) = 42 + 79 = 121 and 97 + flip(42) = 97 + 24 = 121. 11 + flip(1) = 12 and 1 + flip(11) = 12. The total is 4 + 2 = 6. Example 2 nums = [12,21] return = 2 The pair (0, 1) is not valid: 12 + flip(21) = 24, while 21 + flip(12) = 42. Only (0, 0) and (1, 1) are counted. Example 3 nums = [2050,2160,800] return = 4 The reversed values are [502,612,8]. The pair (0, 1) is valid because 2050 + 612 = 2160 + 502 = 2662. Together with the three self-pairs, this gives 4. The zero inside 502 must be preserved. Constraints 1 <= nums.length <= 10^5. 0 <= nums[i] <= 10^9. The answer is an exact integer and fits in a signed 64-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Rearrange the equation. nums[i] + flip(nums[j]) = nums[j] + flip(nums[i]) becomes nums[i] - flip(nums[i]) = nums[j] - flip(nums[j]). So each element has a key, x - flip(x), and a valid pair is any two indices with the same key. Don't use two pointers on the raw array. Compute keys, then count with a hash map as you scan. For each element, add the current frequency of its key plus one for the self-pair, then increment the frequency. Equivalent: a group of size k gives k*(k+1)/2 pairs. The pitfalls are overflow, so use a 64-bit long, and writing flip wrong. Strip leading zeros by building the number with value = value*10 + digit, which handles 800 to 8 automatically. Total work is O(n * digits). If the algebra doesn't click under pressure, StealthCoder is the hedge during the live OA.
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 Count Reverse-Digit 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 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 Visa's OA.
Visa 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.
Count Reverse-Digit Pairs FAQ
What's the trick in Count Reverse-Digit Pairs?+
Move the flip terms to opposite sides. The condition becomes nums[i] - flip(nums[i]) equals nums[j] - flip(nums[j]). Once you see that, it's just counting equal keys. A hash map of key frequencies solves it in one pass.
Do I really need two pointers here?+
No. The hint says two-pointers, but a hash map on x - flip(x) is simpler and linear. Sorting by key and walking groups also works, but it adds a log factor you don't need.
How do I handle self-pairs and overflow?+
Since i <= j is allowed, every index pairs with itself. While scanning, add the existing count of the key plus one, then increment. Store the total in a 64-bit long. With 10^5 elements the answer can exceed 2^31.
How should I implement flip correctly?+
Loop over digits: result = result * 10 + x % 10, then x /= 10. Leading zeros of the reversed number disappear naturally, so flip(800) is 8. Inner zeros stay, so flip(2050) is 502. flip(0) returns 0. Test against the three examples.
How do I prep for this in 48 hours?+
Practice the pattern of rewriting a pair condition into one key per element, then counting with a hash map. Do a couple of problems like that, and hand-trace the examples here. Know the k*(k+1)/2 formula for groups including self-pairs.