Reported September 2026
TikTokhash table

Count One-Swap Number Pairs

Reported by candidates from TikTok's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live TikTok OA. Under 2s to a working solution.
Founder's read

TikTok reported this one in September 2026, and the input size is the first thing to read. Up to 10^4 numbers means about 50 million pairs if you compare every one, and each comparison costs digit work on top. That's the trap. The pattern is hash-table plus counting: generate every one-swap variant of each number, then count matches as you go. If you've got the OA in a day or two, this is the idea to lock in. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the approach below is short enough to remember.

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 &le; numbers.length &le; 10^4
1 &le; numbers[i] &le; 10^9

Reported by candidates. Source: FastPrep

Pattern and pitfall

Numbers have at most 10 digits, so each one has at most 45 swaps. Walk the array left to right and keep a hash map of value to count. For each number, build a set of reachable values: itself plus every valid single-swap result. Skip any swap that puts 0 in front. Using a set matters, because different swaps can give the same value and you must count each earlier index once. Add the map count for every distinct value in the set to the answer, then increment the map for the current number. The pitfall is double counting, either from duplicate swap results or from counting both directions. Example 2 shows it: [123,321,123] gives 3. Swapping is symmetric, so looking back at earlier numbers only is correct. Total work is about 10^4 times 45 string operations, which is trivial. If the leading zero rule slips your mind under pressure, StealthCoder can catch it live.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

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 StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass TikTok's OA.

TikTok 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 drop pair comparison. The difficulty is the edge cases: duplicates, the leading zero rule, and counting each index pair once. Most failures come from those, not the algorithm.

What's the trick to avoid brute force?+

Don't compare pairs. For each number, generate all values reachable with at most one swap, at most 45 of them. Look those up in a hash map of earlier numbers and add their counts. That's roughly 4.5 x 10^5 swaps total.

Why use a set for the swap results?+

Different swaps can produce the same number, like swapping equal digits or two swaps landing on the same value. If you add the map count once per swap, you overcount. Dedupe the variants per number, then sum the counts.

How do I handle leading zeros?+

After a swap, if the first character is 0, discard that variant. Valid results must keep the same digit count. For example, 1650 swapping the 1 and 0 gives 0651, which is invalid and shouldn't match 651.

How do I prepare for this in 48 hours?+

Practice the pattern: hash map of counts, process left to right, add matches before inserting the current value. Write the swap generator on strings. Then test on [123,321,123] and the TikTok example with 1650 and 651 to check edge cases.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with TikTok.

OA at TikTok?
Invisible during screen share
Get it