Data Structures & AlgorithmsVery hardProduct-company MCQ pattern

1, 2, 3, 4 are pushed onto a stack in this order, with pops allowed at any time. How many of the 24 permutations can be produced as the pop sequence?

  1. A. 24
  2. B. 16
  3. C. 14
  4. D. 10

Correct answer: C. 14

The valid push/pop orders are counted by the Catalan number C₄ = (1/5)·C(8, 4) = 14, and a brute-force check over all 24 orders gives the same. Ten permutations, such as 3 1 2 4, are impossible.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic