Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many integers in [1, 10^4] are divisible by 3 and have an odd number of digits? Run the code.

Problem: How many integers in [1, 10^4] are divisible by 3 and have an odd number of digits? Run the code.
print(sum(1 for x in range(1, 10001) if x % 3 == 0 and len(str(x)) % 2 == 1))
What is printed?
  1. A. 303
  2. B. 3333
  3. C. 3030
  4. D. 300

Correct answer: A. 303

Odd digit counts in range are 1, 3 and 5 (10000 itself is not a multiple of 3): 3 + 300 + 0 = 303. 3333 counts every multiple of 3, 3030 counts the even-length ones, and 300 forgets the single digits 3, 6, 9.

More Coding Interview Problems questions

Exams that ask this topic