Challenge - 5 Problems
Priority Queue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this priority queue insertion and removal?
Consider a priority queue where lower numbers have higher priority. What will be the printed order after inserting 4, 1, 3, and then removing two elements?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { var pq = new PriorityQueue<int, int>(); pq.Enqueue(4, 4); pq.Enqueue(1, 1); pq.Enqueue(3, 3); Console.Write(pq.Dequeue() + " -> "); Console.Write(pq.Dequeue() + " -> "); Console.WriteLine("null"); } }
Attempts:
2 left
💡 Hint
Remember, lower priority value means higher priority in this queue.
✗ Incorrect
The priority queue uses the priority value to decide order. Since 1 has priority 1, it comes out first, then 3 with priority 3.
🧠 Conceptual
intermediateWhich statement best describes a priority queue?
Choose the best description of a priority queue.
Attempts:
2 left
💡 Hint
Think about how priority affects removal order.
✗ Incorrect
A priority queue removes elements based on priority, not insertion order.
🔧 Debug
advancedWhat error does this priority queue code produce?
What error will this C# code produce when run?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { var pq = new PriorityQueue<int, int>(); pq.Enqueue(5, 5); pq.Enqueue(2, 2); pq.Enqueue(8, 8); Console.WriteLine(pq.Dequeue()); Console.WriteLine(pq.Dequeue()); Console.WriteLine(pq.Dequeue()); Console.WriteLine(pq.Dequeue()); } }
Attempts:
2 left
💡 Hint
Check what happens when you dequeue more elements than present.
✗ Incorrect
Dequeuing from an empty priority queue throws InvalidOperationException.
🚀 Application
advancedHow many elements remain after these operations?
Start with an empty priority queue. Insert elements with priorities: (10, 2), (20, 1), (30, 3). Then remove one element. How many elements remain?
Attempts:
2 left
💡 Hint
One element is removed after inserting three.
✗ Incorrect
After inserting 3 elements and removing 1, 2 remain.
❓ Predict Output
expertWhat is the output after mixed enqueue and dequeue operations?
Given a priority queue where lower priority number means higher priority, what is the output of this code?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { var pq = new PriorityQueue<string, int>(); pq.Enqueue("apple", 5); pq.Enqueue("banana", 3); pq.Enqueue("cherry", 4); Console.Write(pq.Dequeue() + " -> "); pq.Enqueue("date", 2); Console.Write(pq.Dequeue() + " -> "); Console.Write(pq.Dequeue() + " -> "); Console.WriteLine(pq.Dequeue() + " -> null"); } }
Attempts:
2 left
💡 Hint
Track the priority values carefully after each operation.
✗ Incorrect
First dequeue removes banana (priority 3). Then date (priority 2) is added. Next dequeue removes date (priority 2), then cherry (priority 4), then apple (priority 5).
