A priority queue is used to implement a stack that stores characters. PUSH (C) is implemented as INSERT where is an appropriate integer key chosen by the implementation. POP is implemented as DELETEMIN. For a sequence of operations, the keys chosen are in
GATE CSE 1997Data Structures › Heaps & Priority QueuesVerified
GATE CSE 1997 PYQ: A priority queue Q Q Q is used to implement a stack that stores characters. PUSH (C) is implemented as…
- A
non-increasing order
- B
non-decreasing order
- C
strictly increasing order
- DCorrect answer
strictly decreasing order
Answer
(D) strictly decreasing order
Approach
A stack follows LIFO: the most recently pushed element must be popped first. Since POP is DELETEMIN(Q), the most recently inserted element must have the smallest key. Therefore, each new PUSH must be assigned a key smaller than all previously assigned keys.
For the priority queue to mimic a stack, each new push must become the highest-priority (minimum-key) element. Hence the implementation must assign strictly decreasing keys to successive PUSH operations.
Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.