Question
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 time MIN operation that returns a pointer to the record with smallest key present in the stack
- without deleting the corresponding record, and
- without increasing the complexities of the standard stack operations.
Which one or more of the following approach(es) can achieve it?
Official answer and solution
- Official answer
- A
- From
- Official GATE 2025 answer key
Solution
Check which augmentation maintains worst-case time for PUSH, POP, and MIN even when the minimum element is popped.
- (A) is CORRECT: If every record in the stack stores a pointer to the minimum record among all elements below (or at/below ), then:
MIN()compares the top record's key with the key pointed to by its min-pointer in time.PUSH(x)setsx's min-pointer to the current stack minimum (found in time via the old top record) in time.POP()simply pops the top record in time, and the new top record already stores the pointer to the minimum below it.
- (B) fails: When the minimum record is popped, a single pointer cannot recover the next smallest element in time (scanning the stack takes ).
- (C) and (D) fail: Maintaining a sorted array takes per
PUSH/POP, and maintaining a Min-Heap takes perPUSH/POP, violating condition (2).
Option (A)
Selecting (B) without considering what happens when the minimum element is popped from the stack.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2025 Computer Science and Information Technology - Session 2 (CS2)
- Question
- 45 of 65
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.