Data Structures & AlgorithmsHardGATE CS · TCS NQT

Numbers 1 to 5 are pushed in order onto a stack, and pops are interleaved so that the output is 4, 3, 5, 2, 1. How many pops are performed before the value 5 is pushed?

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

Correct answer: B. 2

Simulate pushes 1..5 with pops; the pop of 4 and 3 happens after 4 is pushed and before 5 is pushed. Only these two pops occur before 5 is pushed.

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

Exams that ask this topic