ORIGINAL CODING QUESTION
Digit ink for ticket numbers
Tickets are numbered 1 to x and printed in plain decimal without leading zeros. Return how many times the digit d is printed in total. Every occurrence counts, so ticket 11 prints the digit 1 twice.
Original exercise · hard. Editorial role relevance does not imply a company has asked this question.
Input and output
Two integers x and d. Print the total count.
Constraints
1 ≤ x ≤ 200,000; 0 ≤ d ≤ 9.
Example 1
Input
13 1
Output
6
The digit 1 appears in 1, 10, 11 (twice), 12 and 13: six times.
Example 2
Input
20 0
Output
2
Only 10 and 20 contain a zero.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
Count digit by digit: how often does d appear in the ones place, the tens place, and so on?
Hint 2
For place value p, split x into the part above the place, the digit at the place, and the part below it.
Hint 3
With high = floor(x / (10p)), cur = floor(x / p) % 10 and low = x % p, the place contributes high * p, plus p if cur > d or low + 1 if cur equals d; for d = 0 use (high - 1) * p instead and skip places where high is 0.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →