Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: What is the number of integers in [1, 1000] whose digits are all odd? Run the code.

Problem: What is the number of integers in [1, 1000] whose digits are all odd? Run the code.
print(sum(1 for x in range(1, 1001) if all(int(c) % 2 == 1 for c in str(x))))
What is printed?
  1. A. 125
  2. B. 30
  3. C. 124
  4. D. 155

Correct answer: D. 155

Odd-digit numbers have 1, 2 or 3 digits from {1,3,5,7,9}: 5 + 25 + 125 = 155. 125 counts only 3-digit ones, 30 only 1- and 2-digit ones, and 124 counts all-even-digit numbers.

More Coding Interview Problems questions

Exams that ask this topic