Data Structures & AlgorithmsVery hardTCS Digital · Infosys SP

Items K, L, M, N, O, P 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. L K O M P N
  2. B. M N L K P O
  3. C. O N P M L K
  4. D. L K N M P O

Correct answer: A. L K O M P N

Simulate greedily: push until the needed item is on top, then pop. In L K O M P N, when M must be popped, N is on top of it (pushed later and not yet popped), so M cannot come out. The other sequences pass the simulation.

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

Exams that ask this topic