GATE / CS ARCHIVE

GATE CSE 2025 SET 2 / QUESTION 45

GATE CSE 2025 Set 2 Q45

Data Structures · Stacks & Queues · Full GATE CSE 2025 Set 2 paper

GATE 2025 Set 2, Q45 2 marks · MSQ
Report issue

Stacks & Queues

Question 45, GATE 2025: A stack stores records with distinct positive integer keys. Add MIN, which returns a pointer to the minimum-key record without removing it, in O(1) time while keeping PUSH and POP O(1). Which proposed augmentations suffice?
A. Every record stores a pointer to the smallest-key record below it.
B. Store only one pointer to the current minimum record.
C. Maintain an auxiliary sorted array of all present keys.
D. Maintain a min-heap of all present keys.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: A

A preserves the minimum of every stack prefix, which is the information needed after future pops. For an empty stack use a null pointer. When pushing a new record r onto old top t, compare t.key with the key at t's saved below-minimum pointer, if present. The smaller of those records is the minimum of the entire old stack, so store its pointer in r. This takes O(1). MIN compares the current top with its saved below-minimum and returns the smaller. POP simply exposes the previous top, whose saved pointer is still valid because stack deletion has removed nothing below it. Thus all three operations remain O(1).

B alone supports a new minimum on PUSH but loses the information needed when POP removes the current minimum. Finding the next minimum can require scanning the remaining stack. C permits constant-time access to the smallest sorted entry, but inserting an arbitrary new key can require Θ(n) shifts. D exposes its minimum in O(1), but maintaining the heap on insertions and deletions costs O(log n), not O(1) worst case. Those structures do not meet all requirements merely because their MIN operation is fast. Select A only.

Original paper, page 34 · Answer key