Data Structures & AlgorithmsHardGATE CSE · Stacks

The numbers 1, 2, 3, 4, 5, 6 are pushed onto an empty stack in this order, with pops allowed at any time between pushes. Which of the following output sequences is NOT possible?

  1. A. 3 2 5 6 4 1
  2. B. 3 4 6 5 1 2
  3. C. 4 3 2 1 6 5
  4. D. 1 3 5 4 2 6

Correct answer: B. 3 4 6 5 1 2

After popping 3, 4, 6, 5 the stack holds 1 below 2, so 2 must come out before 1; the sequence ending 1 2 is impossible.

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

Exams that ask this topic