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?
- A. 495
- B. 715
- C. 126
- 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
- Problem: What is the maximum value of a[i] XOR a[j] over i < j for a = [3, 10, 5, 25, 2, 8, 17]? Run the code. python a = [3, 10, 5, 25…
- Problem: How many subarrays of [1, -1, 1, -1, 1] have sum 0? Run the prefix-count code. python from collections import Counter a = [1, -1…
- Problem: What is the number of paths from (0,0) to (4,4) using right and up moves that never touch the diagonal cells (i, i) except at the…
- Problem: What is the number of integers in [1, 10000] that are perfect squares and also end in the digit 9? Run the code. python…
- Problem: How many ways are there to split the string '1234' into two non-empty parts whose integer values are both divisible by 3? Run the…
- Problem: How many non-empty subsets of {1, 2, 3, 4, 5, 6, 7, 8} have a sum that is a multiple of 4? Run the code. python from itertools…