GATE Computer ScienceVery hardGATE CS (PYQ pattern)

A min-stack keeps an auxiliary stack: a value is pushed on it when it is less than or equal to the current auxiliary top (or it is empty); on a pop, if the popped value equals the auxiliary top it is popped there too. The operations are push 5, 7, 5, 3, 3, pop, pop, push 9, 2, pop, pop, pop, push 1. How many pushes were made on the auxiliary stack in total, and what is its final size?

  1. A. 5 pushes, final size 2
  2. B. 6 pushes, final size 2
  3. C. 6 pushes, final size 3
  4. D. 8 pushes, final size 1

Correct answer: B. 6 pushes, final size 2

With the <= rule duplicates of the minimum are also stored. Tracing the operations gives 6 auxiliary pushes and a final auxiliary stack [5, 1].

More Programming and Data Structures (GATE) questions

Exams that ask this topic