C++ Priority Queue

Last Updated :
Discuss
Comments

Question 1

What will be the output of the following C++ program?

#include <iostream>
#include <queue>
using namespace std;
int main()
{
int arr[]={1,2,3};

priority_queue<int> pq1(arr, arr+3);  
cout<<"Max priority queue: ";
while(pq1.empty()==false){
cout<<pq1.top()<<" ";
pq1.pop();
}
cout<<endl;


for(int i=0; i<3; i++){
arr[i]=-arr[i];    
}

priority_queue<int> pq2(arr, arr+3);

cout<<"Min priority queue: ";
while(pq2.empty()==false){
cout<<-pq2.top()<<" ";
pq2.pop();
}

}
  • Max priority queue: 3 2 1 
    Min priority queue: 1 2 3 

  • Max priority queue:  2 1 
    Min priority queue:  2 3 

  • Max priority queue: 3 2 
    Min priority queue: 1 2 

  • Error

Question 2

What will be the output of the following C++ program?

#include <iostream>
#include <queue>
using namespace std;
int main()
{
priority_queue<int> mypqueue;
mypqueue.emplace(10);
mypqueue.emplace(20);
mypqueue.emplace(30);

cout << "mypqueue = ";
while (!mypqueue.empty()) {
 cout << mypqueue.top() << " ";
 mypqueue.pop();
}
return 0;
}
  • mypqueue = 30 20 10 

  • mypqueue = 10 20 30 

  • mypqueue =20 10 

  • Error

Question 3

With what data structure can a priority queue be implemented in C++?

  • List

  • array

  • Heap

  • Linked list

Question 4

Which of the following STL containers can be used to implement a priority queue?

  • list

  • vector

  • deque

  • All of the above

Question 5

How can you create a priority queue in C++ that stores elements of a custom data type?

  • By using the default ordering of elements in a priority queue

  • By using a custom comparison function

  • By using the greater<T>() function as the comparison function

  • By using the less<T>() function as the comparison function

Question 6

What is the time complexity of inserting an element into a priority queue in STL?

  • O(1) 
     

  • O(nlog n)

  • O(log n)

  • O( n)

Question 7

Which of the following is NOT a common application of priority queues?

  • Scheduling tasks in a computer operating system

  • Implementing Dijkstra's algorithm for finding the shortest path in a graph

  • Implementing a bubble sort algorithm for sorting a list of elements

  • Implementing a Huffman coding tree for data compression

Question 8

Which of the following is NOT a common application of priority queues?

  • Scheduling tasks in a computer operating system

  • Implementing Dijkstra's algorithm for finding the shortest path in a graph

  • Implementing a bubble sort algorithm for sorting a list of elements

  • Implementing a Huffman coding tree for data compression

Question 9

Which of the following is the correct syntax to remove the top element from a priority queue in STL?

  • pq.pop_front();

  • pq.pop_back();

  • pq.pop();

  • None of the above

Question 10

Which of the following statements is NOT true about priority queues in STL? 

  • A priority queue stores elements in decreasing order by default.

  • Inserting an element into a priority queue has a time complexity of O(1)

  • Removing the top element from a priority queue has a time complexity of O(log n).

  • A priority queue can be implemented using any of the container classes vector, list, or deque.

Tags:

There are 10 questions to complete.

Take a part in the ongoing discussion