Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice

Problem: How many integers in [1, 999] have digits that are strictly increasing (each digit larger than the previous)? Run the code.

Problem: How many integers in [1, 999] have digits that are strictly increasing (each digit larger than the previous)? Run the code.
print(sum(1 for x in range(1, 1000) if all(int(str(x)[i]) < int(str(x)[i + 1]) for i in range(len(str(x)) - 1))))
What is printed?
  1. A. 129
  2. B. 219
  3. C. 165
  4. D. 120

Correct answer: A. 129

Strictly increasing digits from 1..9: C(9,1) + C(9,2) + C(9,3) = 9 + 36 + 84 = 129 (0 cannot lead). Non-decreasing gives 219, allowing a leading 0 gives 165, and C(10,3) = 120 counts only 3-digit picks including 0.

More Coding Interview Problems questions

Exams that ask this topic