GateAI
Stacks

GATE CSE 2025 Set 2 · Q45

MSQ2 marksMedium

Time

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?

Select all that applyOne or more correct

Pick every correct option

Solution

Step-by-step verified solution

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

Stacks

To support an $O(1)$ timeMIN operation without increasing the time complexity of standard stack operations (PUSH and POPmust both remain $O(1)$):

  • Option A: By storing with each record in the stack a pointer to the minimum element present at or below that record's position, we can update the pointer during PUSH in O(1)O(1) time (comparing the new key with the minimum below it). During POP, the stack simply shrinks, automatically exposing the previous minimum at the new top in $O(1)$ time.MIN simply returns the pointer stored at the top of the stack in O(1)O(1) time. Thus, (A) is correct.

  • Option B: If only a single global pointer to the current minimum record is maintained, when that minimum record is popped, finding the new minimum requires scanning the remaining elements of the stack, which takes O(n)O(n) time and increases the complexity of POP. Thus, (B) is incorrect.

  • Option C: Maintaining a sorted auxiliary array requires O(n)O(n) time to insert or remove elements, increasing the complexity of PUSH and POPto $O(n)$. Thus,(C) is incorrect.

  • Option D: Maintaining a Min-Heap requires O(log⁡n)O(\log n) time per PUSH (insert) and POP (delete), increasing the complexity of standard stack operations from O(1)O(1) to O(log⁡n)O(\log n). Thus, (D) is incorrect.

Hence, only (A) achieves the required complexities.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Stacks previous year question is Q45 of the GATE CSE 2025 Set 2 paper. Find more GATE CSE PYQs on Stacks in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 3 related Stacks PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Stacks in GATE CSE

Stacks has 3 GATE CSE PYQs across 3 papers from 2015 to 2025. 2 of them are 2-mark MSQs like this one, and it is the only one in GATE CSE 2025 Set 2.

How GATE tests Stacks

More from GATE CSE 2025 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2025 Set 2 · Q45 · MSQ

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?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.