Data Structures & AlgorithmsVery hardGATE CS · Product placement

Items P, Q, R, S, T, U are pushed onto an empty stack in this order, and a pop may happen at any time. Which pop order is NOT possible?

  1. A. Q S R P U T
  2. B. P Q R S T U
  3. C. T S R U Q P
  4. D. S R Q U P T

Correct answer: D. S R Q U P T

Simulate greedily: push until the needed item is on top, then pop. In S R Q U P T, when P must be popped, T is on top of it (pushed later and not yet popped), so P cannot come out. The other sequences pass the simulation.

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

Exams that ask this topic