Skip to main content

GATE CSE 2025Data Structures › Stacks, Queues, Linked Lists & ArraysVerified

GATE CSE 2025 PYQ: ​​​Consider a stack data structure into which we can PUSH and POP records. Assume that each record pushed in…

​​​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)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. A
    Keep with every record in the stack, a pointer to the record with the smallest key below it.
    Correct answer
  2. B
    Keep a pointer to the record with the smallest key in the stack.
  3. C
    Keep an auxiliary array in which the key values of the records in the stack are maintained in sorted order.
  4. D
    Keep a Min-Heap in which the key values of the records in the stack are maintained.

Answer

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

Approach

The requirement is a MIN that returns a pointer to the minimum-key record in O(1) worst case, while PUSH and POP keep their standard O(1) cost and the minimum record is not removed. So we must check, for each proposed augmentation, whether MIN is O(1) AND whether PUSH/POP stay O(1) — especially POP, which can remove the current minimum. Option A is the classic 'store the running minimum with each node' trick; the others break either the MIN bound or the PUSH/POP bound.

Only keeping, with each record, a pointer to the smallest key below it lets the stack report the minimum in O(1) while preserving O(1) PUSH and POP. The minimum is always recoverable from the top record alone, and when the minimum is popped the previous records' stored pointers already contain the correct next minimum, so no rescanning is needed. The other three schemes either make MIN's maintenance cost O(n)/O(log n) on PUSH/POP (sorted array, heap) or make POP O(n) when the minimum is removed (single min pointer).

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.