GATE Computer ScienceHardGATE CSE

Integers 1, 2, 3, 4, 5, 6 are pushed onto a stack in this order, with pops allowed at any time. How many distinct complete output sequences have 1 as the very last element popped?

  1. A. 43
  2. B. 40
  3. C. 42
  4. D. 50

Correct answer: C. 42

If 1 leaves last, all of 2..6 are pushed and popped while 1 sits at the bottom: the outputs correspond to stack-permutations of 5 elements, Catalan(5) = 42.

More Programming and Data Structures (GATE) questions

Exams that ask this topic