Data Structures & AlgorithmsModerateGATE CS · TCS NQT

The integers 1, 2, 3, 4, 5, 6 are pushed onto an initially empty stack in this order, and pops may be interleaved with pushes at any time. Which of the following output sequences is NOT obtainable?

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

Correct answer: C. 2 4 3 6 1 5

In 2 4 3 6 1 5, once 6 is popped, 5 lies above 1 in the stack, so 5 must leave before 1. The output 6, 1, 5 has the forbidden pattern c, a, b (a < b < c) and is impossible; the other three sequences can each be produced.

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

Exams that ask this topic