The Gateway to Computer Science Excellence
+34 votes

A priority queue $Q$ is used to implement a stack 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

  1. non-increasing order

  2. non-decreasing order

  3. strictly increasing order

  4. strictly decreasing order

in DS by
edited by | 5.8k views

$Values: 1,1,2,3,4$




We need to choose keys in striclty decreasing order in order to satify $STACK$ property as well as $DELETE-MIN(Q)$

3 Answers

+33 votes
Best answer
Implementing stack using priority queue require first element inserted in stack will be deleted at last, and to implement it using deletemin() operation of queue will require first element inserted in queue must have highest priority.

So the keys must be in strictly decreasing order.

Correct Answer: $D$
edited by

@srestha mam priority of element doesn't depend always on the value of the element but the right thing is it depends on the priority of the element that's the reason it is known as a priority queue. 

A binary tree that stores priorities (Or priority element) pairs at nodes

In your case, you have taken min-heap, but before that please look at what is given insert function 

PUSH (C) is implemented as INSERT (Q,C,K) where K is an appropriate integer key chosen by the implementation. 

Clearly, we can see here node is taken as pair. So a node is taken as pairs <element, K(priority)> and min-heap finds out not by the element of a node but by priority of that node.

And Coming to the question:

Then why it is named DELETEMIN()??

It is delete-min because it finds the minimum value of priority not the minimum value of an element, here case is different

See this Nptel video for a clear idea: 


@Rishi yadav

Was I saying correct ? If no what was the error.


Yes @Satbir


@Rishi yadav

they are asking for a sequence of operation, that means 

i) In time of pushing, they are using "strictly decreasing order sequence" or

II)at the time of popping. they using "strictly decreasing order sequence"??

Another thing

DeleteMin(Q) actually deleting priority-wise-minimum key. right??

So, it has disadvantage of deletion of O(n) time. rt??


at the time of pushing

@srestha mam what disadvantage


@srestha mam have you seen the video

why not popping??
because it is operating on key not element
yes, video is about priority queue, but this question is an implementation of priority queue.

@srestha mam i have shared with timing please check that once

what timing??

just play the video given above @srestha mam

I have heard this lecture.

there clearly mentioned 

the nodes as containing the priority of the element and the element is sitting somewhere here also. 


@srestha mam if still, your doubt is not clear then this will clear

So D is the correct option no doubt about that

CLRS Book page:163-164


@Rishi yadav

I cannot agree with u still now.

Even see here , they are also disagree why key will be stricly decreasing

(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.

So, option A) canceled.

Now ,

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

and  according to stack overflow,

Priority queue, maintain heap property. According to this elements must be ordered

So, elements either return max-heap or min-heap. And for this not unsorted elements will be allowed here.  And as we delete minimum element first, So, insertion of element must be decreasing order.


priority queue nothing but heap here.In ur diagram is it maintaining heap property??

Plz chk the links here

I hope all doubt now gone for this question.



He has given you the link to the NPTEL video, CLRS what more do you want ?

I Don't want to discuss it anymore.

+14 votes
Here we need to implement stack .

For it last element inserted must be poped  first.

Priority Q can be implemented by min heap or max heap but here priority Q operation is given deleteMin that means in the sense of stack minimum element having the higher priority that means here we are talking about min heap .


Means first element of heap should be lastly inserted that means keys must be inserted in decreasing order.

Since it is priority Q and two element doesn't have same priority so strictly decreasing order.
One doubt key can be 1 1 2 3 4(As there not mentioned repeated element not allowed)

So, in that case is not the answer will be A) means ans simply be non increasing order



in the queue the elements are present in the following manner:-

1 | 2 | 3 | 4 | 5

now rear will point to 1 and front will point to 5, according to the rule of queue then it delete 5.

but how deletemin () is deleting the elements from rear??

I have drawn it in fig

If figure has any problem?

Hi papesh, A priority queue Q is used to implement a stack. It means we are going to perform operations on a priority queue to simulate it like a stack. As we require deletemin(), going for min-heap is a better choice.

Lets say sequence is in increasing order, and we are implementing a min-heap using an array. Let the elements be 1,2,3,4,5,6,7. This key arrangement is already in a min heap format and it will simulate stack where pop operation deletes minimum element. Now even if add one element lets say 8, it will go to end of the array and while popping its still going to pop minimum element. So the overall arrangement is increasing order.

but the answer says it should be in strictly decreasing order. What should have been the correct way of solving it and where am I making mistake?

"Means first element of heap should be lastly inserted that means keys must be inserted in decreasing order."  Also I am not clear with this statement.


@srestha mam elements can be in non-increasing order but keys K should be in strictly decreasing order

What is difference between elements and key, I havenot got.
you mean priority of element as key, that represents as k here.


But, if value(represent by C) is non increasing order, then priority also can be non-increasing order.
priority can not be in non-increasing order

see this 


void Stack::push(int n){
pq.push(pi(cnt, n));

Is there any possiblity cnt have same value while insertion


counting element of stack and pushing element such that element delete min element from stack first, are not same thing.

delete min is deleting that element which has minimum priority not the minimum value

srestha mam see this  code here and there typedef pair<int, int> pi; is used it means value and priority have taken seperately


@Rishi yadav

In ans , what is meaning of this line?

queue will require first element inserted in queue must have highest priority.


@Rishi yadav

U have taken  Rear element as lowest priority. But , they have taken it as highest priority


@srestha mam 

they have taken first element as highest priority (front point here) it means element at rear will be lowest priority there too

first element inserted in queue must have highest priority. see selected answer @srestha mam

@Rishi yadav

tell me this

When we implement queue to implement a stack, We need atleast 2 queue. 


But here we implementing with one priority queue. Now if we  insert 1,1,2,3,4  then according to stack it should remove 4,3,2,1,1 


Then how min delete first?? 4 will remove first according to stack.


Secondly according to ur dia, if $4$ remove first, then lowest priority element will remove first. That means priority comes in min-delete 1,2,3,4,5 , which will be an increasing order. 



@srestha mam 

INSERT $(Q,C,K)$ where $K$ is an appropriate integer key chosen by the implementation. 



@srestha mam check this insertion by decrementing key(k) and deletion by incrementing the index(key k)

That is not maintaining stack property then.  Because in stack which inserted last should remove first

@srestha yes mam diagram was wrong


@Rishi yadav

Have u got correct diagram for it?? Though ur logic is valid I think.


I think priority queue implementation is nothing but 'Min-heap' implementation. So, if we insert values like 5,4,3,2,1 it always give 1,2,3,4,5 as output. right??

So, you mentioned priority separately, but that is not needed. right??

But one thing, if priority queue takes max heap as representation , then D) will not be answer. right?? So, what about max-heap??

Check here:



Yes mam you are right,


+8 votes

option A vs option D

In a stack, the priority of each inserted element is monotonically increasing; thus, the last element inserted is always the first retrieved. In a queue, the priority of each inserted element is monotonically decreasing; thus, the first element inserted is always the first retrieved.

Explanation :- A priority queue works on the bases of it's element's priority , it doesn't do operation in FIFO or LIFO manner like normal queue and stack do. Like here we defined some operation like DELETEMIN(Q) , so this abstract  data structure will help to delete minimum priority element from priority queue , noe it doesn't matter in which order the element are inserted.

like if elements are like 5,3,2,7,8 DELETEMIN(Q) will delete 2 (see no FIFO).We have to here implement stack using Priority queue (although practically it's not a good approach to implement stack using priority queue)so here element must follow LIFO mechanism. Now if elements are in strictly decreasing order we can implement stack using priority queue successfully but when elements are repeated (non-increasing order) then our priority queue will behave like a normal queue and it will follow FIFO approach but to implement stack element  have to follow LIFO approach , which won't be possible.

elements : $3_{a} , 3_{b},3_{c},3_{d}$    // all elements are same , to take care about order of insertion i named them like $3_{a} , 3_{b}$ and so on...

Here i'm taking the help of  

to implement stack using priority queue. the key concept is add a count to each variable which will indicate when the element was inserted (think like time)

$3_{a}$ is pushed into stack at t=1

$3_{b}$  is pushed into stack at t=2

$3_{c}$ is pushed into stack at t=3

$3_{d}$ is pushed into stack at t=4

we implement PUSH as






So stack is like

$3_{d}$    (t=4)
$3_{c}$    (t=3)
$3_{b}$    (t=2)
$3_{a}$    (t=1)

We implement POP as






So we can see the first element the PQ.pop() will pop is on the bases on FIFO order and it would be $3_{a}$  but it's not what stack will pop (stack would have to follow LIFO so it would have to pop $3_{d}$)  so that's why option A would be false.

which can be an appropriate text book to refer such topics of data structure?

In CLRS, its not mentioned in depth, if I am not wrong. Please someone suggest !
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
52,345 questions
60,484 answers
95,290 users