Data Structures & AlgorithmsHardTCS NQT · Product Company SDE
Problem: How many strings of length 8 made of matched parentheses ( and ) are valid (well-formed)?
- A. 70
- B. 42
- C. 14
- 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
- Problem: A robot is on a 4×4 torus (moving off one edge reappears on the opposite edge) starting at (0,0), using row, column coordinates…
- Problem: Evaluate the postfix expression "3 4 + 2 7 −" using a stack (the operator pops the top two values, computes second-from-top op…
- Problem: Rule 30 cellular automaton on an infinite line starts with a single live cell. Each generation, a cell's new state is determined…
- Problem: How many integers x with 1 ≤ x ≤ 100 have an odd number of set bits in their binary representation AND are odd themselves?
- Problem: What is the minimum number of bit flips needed to convert 29 into 15 (flips of single bits in binary)?
- Problem: What is the largest power of 2 that divides 480 × 36?