Count Three-Digit Numbers with Distinct Digits
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The input range here is capped at three-digit numbers, so there are at most 900 values to check. That's the first thing to notice about this ZipRecruiter OA question, reported in October 2024. Brute force isn't a trap, it's the answer. Loop from left to right, pull out the hundreds, tens and ones digits, and count the numbers where all three differ. It's a math and counting problem dressed up as something harder. The risk isn't the algorithm, it's rushing and botching the digit comparison. If you blank on the day, StealthCoder runs invisibly during the live OA and gives you the solution in real time.
The problem
You are given an inclusive range [left, right] containing only three-digit positive integers. Return how many values in the range have three pairwise distinct decimal digits. Function countDistinctThreeDigitNumbers(left: int, right: int) → int Examples Example 1 left = 876 right = 890 return = 3 The qualifying values are 876, 879, and 890. Example 2 left = 100 right = 105 return = 4 The qualifying values are 102, 103, 104, and 105. Constraints 100 <= left <= right <= 999
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that there's no trick. With 100 <= left <= right <= 999, the loop runs at most 900 times, so O(n) with a constant-time digit check is fine. For each n, compute a = n // 100, b = (n // 10) % 10, c = n % 10. Count it if a != b, b != c and a != c. The common pitfall is checking only adjacent digits, which lets 121 or 575 slip through. Another miss is an off-by-one on the inclusive right bound, so use range(left, right + 1). Check your work against example 2: 100 through 105 gives 102, 103, 104, 105, which is 4, because 100 and 101 repeat a digit. You can also convert to a string and compare len(set(s)) to 3. If you freeze on the live OA, StealthCoder is the hedge that hands you this loop in seconds.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Count Three-Digit Numbers with Distinct Digits 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 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 Three-Digit Numbers with Distinct Digits FAQ
How hard is the ZipRecruiter distinct digits question really?+
Easy. The range tops out at 999, so a plain loop over at most 900 numbers works. The only way to lose points is sloppy digit extraction or forgetting that the right bound is inclusive. Write it, run the two examples, and move on.
What's the trick to counting three-digit numbers with distinct digits?+
There isn't one. Split each number into hundreds, tens and ones digits, then check all three pairs for inequality. Checking only neighbors is the classic mistake, since 121 has non-adjacent repeats. Comparing against a set of the digits is a clean shortcut.
Do I need a math formula instead of a loop?+
No. A formula would count distinct-digit numbers across the full range, but you're given arbitrary left and right bounds. A loop handles any range directly, and 900 iterations is nothing. Reach for a prefix count only if you want to be fancy, which this OA doesn't reward.
Is this pattern still asked in OAs like the October 2024 ZipRecruiter one?+
Yes. Small-constraint counting problems show up regularly because they test clean digit handling and careful reading, not heavy algorithms. Expect variants like different digit counts, or a rule such as no repeated digits plus an extra condition.
How do I prepare in 48 hours for this kind of question?+
Practice digit extraction with // and %, and writing inclusive range loops without off-by-one errors. Then hand-trace both examples from the problem. Spend the rest of your time on harder topics, since this one should take you a few minutes.