Count One-Swap Number Pairs
Reported by candidates from Hudson River Trading's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Hudson River Trading reported this one in September 2026, and it looks like a pair-counting problem until you notice what it really is: a hash map lookup in disguise. Count index pairs where one number becomes the other with at most one digit swap. Equal numbers count too. If you've got an OA coming up, don't try every pair. With 10^4 numbers that's 50 million comparisons, and the swap check makes it worse. StealthCoder is the safety net if you freeze on the live OA, but the idea is short enough to hold in your head.
The problem
You are given an array of positive integers numbers. Count the index pairs (i, j) such that i < j and one number in the pair can be transformed into the other by swapping at most one pair of digit positions. No swap is required when the two numbers are already equal. Each pair of indices is counted once, even when several different digit swaps produce the same value. Use each number's ordinary decimal representation without leading zeroes. A swapped representation that begins with 0 is invalid, so valid transformed numbers have the same number of digits. Return the number of qualifying index pairs. Function countOneSwapPairs(numbers: int[]) → int Examples Example 1 numbers = [1,23,156,1650,651,165,32] return = 3 The qualifying pairs are 23 with 32, 156 with 651, and 156 with 165. Therefore, the result is 3. Example 2 numbers = [123,321,123] return = 3 The two copies of 123 form a qualifying pair without a swap. Each copy of 123 also pairs with 321 by swapping the first and last digits, for 3 pairs in total. Constraints 1 ≤ numbers.length ≤ 10^4 1 ≤ numbers[i] ≤ 10^9
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: each number has at most 9 digits, so it has at most 36 possible single swaps. Flip it around. Walk the array, and for each number, generate every string reachable by one swap, plus the number itself. Put those in a set so duplicates collapse. Then add up how many earlier numbers in a frequency map match each reachable value. Then increment the map for the current number. That's roughly 10^4 times 37 operations. The pitfall is double counting. Two different swaps can yield the same value, which is why you dedupe with a set before summing. Swaps that put a 0 first are invalid, so skip them. Also remember that equal numbers pair without a swap, and the self-value in the set covers that. Don't add the number to itself as a pair. Process the map before inserting the current element. If you blank during the live OA, StealthCoder can supply this map-plus-set structure while you stay calm.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Count One-Swap Number 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
You've seen the question.
Make sure you actually pass Hudson River Trading's OA.
Hudson River Trading 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 One-Swap Number Pairs FAQ
How hard is Count One-Swap Number Pairs really?+
Medium. The idea is simple once you stop comparing pairs. The hard part is the edge cases: duplicates, leading zeros, and different swaps giving the same result. If you handle those three, the code is short and the complexity is easy to defend.
What's the trick to avoid O(n^2)?+
Generate neighbors instead of comparing pairs. Each number has at most 36 digit-swap variants. Store counts of seen numbers in a hash map, and for each new number look up its distinct variants. That turns 50 million pair checks into about 370 thousand operations.
How do I avoid counting the same pair twice?+
Two protections. First, put all reachable values of a number in a set so different swaps that give the same value count once. Second, query the map before inserting the current number, so each index pair (i, j) with i < j is only counted at j.
What about leading zeros after a swap?+
A result starting with 0 is invalid, so skip it. Example: 1650 swapping the 1 and 0 would give 0651, which doesn't count. Check that the first character isn't '0' after swapping before you add the value to the set.
How do I prepare for this in 48 hours?+
Practice the pattern of frequency map plus generated neighbors on a few problems. Then write this one from scratch, and test it on [123,321,123], which should return 3. Treat the test as a check on duplicate handling, since that's where most wrong answers come from.