The GATE Grind

GATE 2025 CS (CS2) – Question 45

Programming and Data Structures · Stacks and Queues · 2 marks · Multiple select

Consider a stack data structure into which we can PUSH and POP records. Assume that each record pushed in the stack has a positive integer key and that all keys are distinct.

We wish to augment the stack data structure with an $O(1)$ time MIN operation that returns a pointer to the record with smallest key present in the stack

1) without deleting the corresponding record, and
2) without increasing the complexities of the standard stack operations.

Which one or more of the following approach(es) can achieve it?

  1. Keep with every record in the stack, a pointer to the record with the smallest key below it.
  2. Keep a pointer to the record with the smallest key in the stack.
  3. Keep an auxiliary array in which the key values of the records in the stack are maintained in sorted order.
  4. Keep a Min-Heap in which the key values of the records in the stack are maintained.

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (A) Keep with every record in the stack, a pointer to the record with the smallest key below it.

Explanation

Storing the running minimum with each record gives O(1) push, pop and MIN. A single min pointer cannot be restored in O(1) after popping the min. A sorted array or a min-heap makes push/pop cost O(n) or O(log n).