Data Structures & AlgorithmsHardTCS NQT · Infosys pattern

The numbers 1, 2, 3, 4, 5 are pushed onto a stack in that order, and pops may happen at any time in between. Which output sequence is IMPOSSIBLE?

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

Correct answer: D. 3 1 2 5 4

To output 3 first, 1 and 2 must already be on the stack with 2 above 1, so 1 cannot come out before 2. The other three can be produced, e.g. 3 2 5 4 1 is push 1,2,3, pop, pop, push 4,5, pop, pop, pop.

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

Exams that ask this topic