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?
Correct : a
The correct answer is Option A.
The goal is to support O(1) MIN without slowing down PUSH or POP. Let''s check each approach.
Option A - Store a min-pointer with every record: When a new element is pushed, compare it with the current top''s min-pointer and store whichever is smaller alongside the new record. MIN is O(1) - just read the top record''s min-pointer. PUSH and POP remain O(1). This works perfectly and is correct.
Option B - Single global min-pointer: Works for PUSH and MIN, but fails for POP. If the minimum element is popped, the pointer becomes stale and finding the new minimum requires a full scan - making POP O(n). Incorrect.
Option C - Sorted auxiliary array: Inserting into a sorted array during PUSH requires O(n) time for shifting. This violates the O(1) PUSH requirement. Incorrect.
Option D - Min-Heap: Heap insertion takes O(log n), not O(1). PUSH complexity increases. Incorrect.
Option A is the classic and correct approach — it essentially maintains a "running minimum" at every level of the stack, so no matter what gets popped, the new top always knows the current minimum instantly.
Similar Questions
Total Unique Visitors