GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Integers 1, 2, 3, 4, 5 are pushed on an initially empty stack in this order; pops may be interleaved with pushes at any time. How many of the possible output sequences begin with 3?

  1. A. 11
  2. B. 14
  3. C. 9
  4. D. 7

Correct answer: C. 9

Outputting 3 first forces push 1, 2, 3 and pop 3. The stack now holds 1, 2 (2 on top) and 4, 5 are still to be pushed; counting the valid completions (2 must leave before 1) gives 9, checked by brute force over all 120 orders.

More Programming and Data Structures (GATE) questions

Exams that ask this topic