← Questions
Count three-digit numbers with distinct digits
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Meta's AI-enabled coding round, last asked Jan 2026. It tests math and counting.
Count the three-digit numbers (100 to 999) whose three digits are all different. Write a brute-force version first, then count directly, and then generalize to an arbitrary range.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
count_distinct_digit_numbers(low: int = 100, high: int = 999) -> int— counts the three-digit numbers in[low, high], both inclusive, whose digits are pairwise different. Parts of the range outside 100–999 are ignored.
Example
count_distinct_digit_numbers() -> 648 # 9 * 9 * 8
count_distinct_digit_numbers(100, 200) -> 72 # 1 _ _: 9 tens choices * 8 units choices; 200 repeats 0
count_distinct_digit_numbers(100, 150) -> 33
count_distinct_digit_numbers(987, 1000) -> 1 # only 987
Constraints
-
0 <= low <= high <= 10^9for the generalized version; only three-digit numbers count.
Clarifying questions
- Is the range inclusive at both ends?
- Do numbers outside 100–999 count at all, or should the generalized version handle any number of digits?
- Is a brute-force loop acceptable, or is a formula expected?
What a strong answer covers
- Brute force: loop over the range, split each number into digits with
% 10and// 10, and compare them.O(range)and easy to trust, so it doubles as a test oracle. - The direct count: 9 choices for the hundreds digit (not 0), 9 for the tens (any digit but the first), 8 for the units, giving 648.
- For a range, count numbers
<= xwith distinct digits and takef(high) - f(low - 1), fixing digits from the left and counting completions, which is a small digit DP. - Edge cases: ranges that start or end outside 100–999,
low > high, single-number ranges. - Testing the formula against the brute force over many random ranges.
Follow-ups
- Count numbers of any length up to
nwith all distinct digits. - Count the numbers in
[1, n]with at least one repeated digit. - Return the
k-th smallest three-digit number with distinct digits without enumerating them.
Where it's asked
- Meta · AI-enabled coding · Last asked Jan 202660 min · Several progressive tasks on a starter codebase in CoderPad with an AI assistant panel; usually two rounds · The first task usually without AI, later tasks with it
What it tests
math · counting
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
