Data Structures & AlgorithmsHardTCS NQT · ICPC regional
Problem: How many non-decreasing strings of length 4 over the digits {0, 1, 2} exist (each digit is at least the one before it)?
- A. 15
- B. 81
- C. 35
- D. 6
Correct answer: A. 15
A non-decreasing string is fixed by how many 0s, 1s and 2s it has: C(4 + 2, 2) = 15. 3⁴ = 81 ignores the order, C(7, 3) = 35 uses the wrong stars-and-bars formula, and C(4, 2) = 6 forgets the stars.
More Coding Interview Problems questions
- Problem: Can the array [1, 5, 11, 5] be split into two subsets with equal sums? Answer 1 for yes and 0 for no.
- Problem: A wooden stick of length 7 must be cut at the marked positions 1, 3, 4 and 5 (measured from one end). Each cut costs the current…
- Problem: The elimination game: numbers 1 to 9 are written in a row. Repeatedly remove alternate numbers, starting from the left, then from…
- Problem: The 3-bit Gray code sequence is 000, 001, 011, 010, 110, 111, 101, 100 for indices 0 to 7. What is the decimal value of the…
- Problem: How many subsets (including the empty set) of [1, 2, 3, 4, 5] have bitwise XOR equal to 0?
- Problem: How many integers from 1 to 100 are divisible by exactly one of 2 and 3?