Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice

Problem: How many 4-digit numbers have all digits equal to or greater than the digit before them (non-decreasing)? Run the code.

Problem: How many 4-digit numbers have all digits equal to or greater than the digit before them (non-decreasing)? Run the code.
print(sum(1 for x in range(1000, 10000) if all(int(str(x)[i]) <= int(str(x)[i + 1]) for i in range(3))))
What is printed?
  1. A. 495
  2. B. 715
  3. C. 126
  4. D. 6561

Correct answer: A. 495

A non-decreasing string of 4 digits from 1..9 is a multiset of size 4 from 9 values, C(12, 4) = 495. Allowing 0 gives C(13, 4) = 715, strictly increasing gives C(9, 4) = 126, and 9⁴ = 6561 ignores the order constraint.

More Coding Interview Problems questions

Exams that ask this topic