Data Structures & AlgorithmsVery hardTCS NQT · Infosys pattern

A min-stack (push/pop O(1), getMin O(1) via an auxiliary stack tracking the running minimum) runs this sequence in order: push(-2), push(-1), push(-4), push(-8), pop(), push(-8), push(6), push(1), getMin(). What does the final getMin() return?

  1. A. -7
  2. B. 6
  3. C. -8
  4. D. 1

Correct answer: C. -8

The auxiliary stack pushes min(new value, current min) on every push and pops in lockstep with the main stack, so its top always holds the minimum of the elements currently on the stack. Tracing all operations in order leaves the minimum as -8; 1 is just the top of the main stack, not the minimum.

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

Exams that ask this topic