Challenge - 5 Problems
Priority Queue Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this priority queue code?
Consider the following C++ code using a max priority queue. What will be printed?
DSA C++
#include <iostream> #include <queue> int main() { std::priority_queue<int> pq; pq.push(10); pq.push(5); pq.push(20); pq.push(1); while (!pq.empty()) { std::cout << pq.top() << " "; pq.pop(); } return 0; }
Attempts:
2 left
💡 Hint
Remember, a max priority queue always returns the largest element first.
✗ Incorrect
The priority queue stores elements so that the largest is always on top. The output prints elements from largest to smallest.
🧠 Conceptual
intermediateWhich statement about priority queues is true?
Choose the correct statement about priority queues.
Attempts:
2 left
💡 Hint
Think about what 'priority' means in a priority queue.
✗ Incorrect
Priority queues return elements based on priority, not insertion order. They can store any data type and allow duplicates.
🔧 Debug
advancedWhat error does this priority queue code produce?
Identify the error when compiling this C++ code snippet.
DSA C++
#include <iostream> #include <queue> int main() { std::priority_queue<int> pq; pq.push(10); pq.push(20); std::cout << pq.pop() << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check the return type of the pop() function in priority_queue.
✗ Incorrect
The pop() function removes the top element but returns void. It cannot be used directly in std::cout.
🚀 Application
advancedHow many elements remain after these operations?
Given a min priority queue with elements inserted as 15, 10, 20, 5, then two pop operations are performed. How many elements remain?
DSA C++
#include <iostream> #include <queue> int main() { std::priority_queue<int, std::vector<int>, std::greater<int>> pq; pq.push(15); pq.push(10); pq.push(20); pq.push(5); pq.pop(); pq.pop(); std::cout << pq.size() << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Each pop removes one element from the queue.
✗ Incorrect
Initially 4 elements are inserted. Two pops remove two elements, so 2 remain.
❓ Predict Output
expertWhat is the output of this custom priority queue comparator code?
Analyze the output of this C++ code using a priority queue with a custom comparator that prioritizes strings by length (shortest first).
DSA C++
#include <iostream> #include <queue> #include <string> #include <vector> struct CompareLength { bool operator()(const std::string& a, const std::string& b) { return a.length() > b.length(); } }; int main() { std::priority_queue<std::string, std::vector<std::string>, CompareLength> pq; pq.push("apple"); pq.push("banana"); pq.push("fig"); pq.push("date"); while (!pq.empty()) { std::cout << pq.top() << " "; pq.pop(); } return 0; }
Attempts:
2 left
💡 Hint
The comparator returns true if the first string is longer, so shorter strings have higher priority.
✗ Incorrect
The priority queue orders strings by length ascending. So shortest string 'fig' comes first, then 'date', then 'apple', then 'banana'.
