Data Structures & AlgorithmsHardTCS NQT · Product Company SDE

Problem: How many strings of length 8 made of matched parentheses ( and ) are valid (well-formed)?

  1. A. 70
  2. B. 42
  3. C. 14
  4. D. 5

Correct answer: C. 14

Valid strings of 4 pairs are counted by the Catalan number C(4) = C(8,4) / 5 = 14. C(8,4) = 70 counts every arrangement of four '(' and four ')', and the neighbouring Catalan numbers 42 (5 pairs) and 5 (3 pairs) come from an off-by-one in n.

More Coding Interview Problems questions

Exams that ask this topic