Reported October 2023
ZipRecruiterstring

Count Pairs Differing in One Digit

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

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

The mistake that sinks a first attempt on this ZipRecruiter problem is counting pairs that differ in one digit but forgetting that identical numbers don't count. The OA was reported in October 2023. You get an array of positive integers and must count index pairs i < j with equal-length decimal strings that differ in exactly one aligned position. It's a string comparison problem dressed up as math. With up to 2000 numbers, the brute force is reachable, which is the trap and the opportunity. If you blank mid-assessment, StealthCoder runs invisibly on screen as a safety net.

The problem

Given positive integers numbers, count index pairs i < j whose ordinary decimal representations have equal length and differ at exactly one aligned digit position.

Function
countOneDigitDifferencePairs(numbers: int[]) → long

Examples
Example 1
numbers = [12,13,22,12]
return = 4
The qualifying index pairs are the first 12 with 13 and 22, 13 with the final 12, and 22 with the final 12.
Example 2
numbers = [1,2,3]
return = 3
Every pair differs at its only digit.

Constraints
0 <= numbers.length <= 2000
1 <= numbers[i] <= 1000000000

Reported by candidates. Source: FastPrep

Pattern and pitfall

With n at most 2000, there are about 2 million pairs, and each number has at most 10 digits. So a direct double loop that converts each number to a string once and compares aligned characters is roughly 20 million character checks. That's fine. Convert everything up front, don't call toString inside the inner loop. For each pair, skip if lengths differ, then count mismatches and bail out once you hit 2. Count only pairs where the mismatch count is exactly 1. The pitfall is treating 12 and 12 as a match. Zero differences doesn't qualify, and example 1 shows the first and last 12 aren't counted. Also return a long, since the count can reach about 2 million and you should not risk overflow in other languages. A faster option groups by length and uses wildcard masks in a hash map, subtracting same-number duplicates. If you freeze live, StealthCoder can hand you the working loop.

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 Pairs Differing in One Digit 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 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 Pairs Differing in One Digit FAQ

How hard is this ZipRecruiter OA question really?+

Easy to medium. The brute force passes under the 2000 element limit, so the difficulty is carefulness, not cleverness. The common failures are counting identical numbers as a match and comparing numbers of different lengths. Get those two right and it's done.

What's the trick to counting pairs that differ in one digit?+

Convert each number to a string once, skip pairs with different lengths, then count mismatched positions. Exit early at two mismatches. Only exactly one mismatch counts. Zero mismatches means equal numbers, which you must exclude.

Do I need an optimized solution or is O(n^2) fine?+

With n up to 2000 and at most 10 digits each, O(n^2 * d) is about 20 million operations. That's comfortable. A wildcard-mask hash map approach works too, but it's more code and more room for duplicate-counting bugs.

Why does example 1 return 4 and not 5?+

The array is [12,13,22,12]. Pairs of 12 with 13, 12 with 22, 13 with the other 12, and 22 with the other 12 all differ in one digit. The two 12s differ in zero, and 13 with 22 differ in two, so neither counts.

How do I prepare for this in 48 hours?+

Write the brute force from memory twice. Test it on [12,13,22,12], [1,2,3], and an empty array. Check the return type is long and that you handle length 0 or 1. Then spend the rest of your time on other string and counting patterns.

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

OA at ZipRecruiter?
Invisible during screen share
Get it