Data Structures & AlgorithmsModerateGATE CS · ICPC regional

Problem: How many binary search trees can be formed with exactly 5 distinct keys?

  1. A. 120
  2. B. 14
  3. C. 132
  4. D. 42

Correct answer: D. 42

The number of BSTs on n keys is the Catalan number C(n) = Σ C(i)·C(n − 1 − i), so C(5) = 42. Counting insertion orders gives 5! = 120, and the neighbouring Catalan numbers 14 and 132 come from an off-by-one in n.

More Coding Interview Problems questions

Exams that ask this topic