Data Structures & AlgorithmsModerate

A min-stack supports push, pop and getMin in O(1). Apply this sequence, starting empty: push 5, push 3, push 7, getMin, pop, push 1, getMin, push 0, getMin, pop, pop, getMin. List the values returned by all the getMin calls, in order (four numbers).

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

Correct answer: C. 3 1 0 3

Each stack entry stores the minimum of everything at or below it, so popping restores the previous minimum. Tracking the all-time minimum ignores pops, and the answer changes once 0 is pushed and then removed.

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

Exams that ask this topic