Skip to main content

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 priority queue QQ is used to implement a stack that stores characters. PUSH (C) is implemented as INSERT (Q,C,K)(Q, C, K) where KK is an appropriate integer key chosen by the implementation. POP is implemented as DELETEMIN(Q)(Q). For a sequence of operations, the keys chosen are in

  1. A

    non-increasing order

  2. B

    non-decreasing order

  3. C

    strictly increasing order

  4. D

    strictly decreasing order

    Correct answer

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.