GATE | GATE CS 1997 | Question 32

A priority queue Q is used to implement a stack S that stores characters. PUSH(C) is implemented as INSERT(Q, C, K) where K is an appropriate integer key chosen by the implementation. POP is implemented as DELETEMIN(Q). For a sequence of operations, the keys chosen are in
(A) Non-increasing order
(B) Non-decreasing order
(C) strictly increasing order
(D) strictly decreasing order

Answer: (D)

Explanation: We are implementing a STACK using Priority Queue. Note that Stack implementation is always last in first out (LIFO) order.

As given “POP is implemented as DELETEMIN(Q)” that means Stack returns minimum element always.

So, we need to implement PUSH(C) using INSERT(Q, C, K) where K is key chosen from strictly-decreasing order(only this order will ensure stack will return minimum element when we POP an element). That will satify Last In First Out (LIFO) property of stack.

That is answer, option (D) is true.

Option (A) non-increasing order can not be true because two same (identical) numbers can not have same priority as priority should be distinguishable for each number.

Quiz of this Question

My Personal Notes arrow_drop_up
Article Tags :


Please write to us at to report any issue with the above content.