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)?

  1. A. 15
  2. B. 81
  3. C. 35
  4. 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

Exams that ask this topic