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 sequence?
Consider a priority queue where lower numbers have higher priority. Insert the numbers 5, 1, 3 in that order. What is the state of the queue after these insertions?
DSA Javascript
class PriorityQueue { constructor() { this.items = []; } enqueue(element) { if (this.isEmpty()) { this.items.push(element); } else { let added = false; for (let i = 0; i < this.items.length; i++) { if (element < this.items[i]) { this.items.splice(i, 0, element); added = true; break; } } if (!added) { this.items.push(element); } } } isEmpty() { return this.items.length === 0; } print() { return this.items.join(' -> ') + ' -> null'; } } const pq = new PriorityQueue(); pq.enqueue(5); pq.enqueue(1); pq.enqueue(3); console.log(pq.print());
Attempts:
2 left
💡 Hint
Remember, lower numbers have higher priority and are placed at the front.
✗ Incorrect
The priority queue inserts elements so that the smallest number is at the front. After inserting 5, then 1, then 3, the queue orders them as 1, 3, 5.
❓ Predict Output
intermediateWhat is the output after dequeue operations on this Priority Queue?
Given a priority queue with elements inserted in this order: 4, 2, 6, 1 (lower number means higher priority). After inserting all, two dequeue operations are performed. What is the state of the queue?
DSA Javascript
class PriorityQueue { constructor() { this.items = []; } enqueue(element) { if (this.isEmpty()) { this.items.push(element); } else { let added = false; for (let i = 0; i < this.items.length; i++) { if (element < this.items[i]) { this.items.splice(i, 0, element); added = true; break; } } if (!added) { this.items.push(element); } } } dequeue() { return this.items.shift(); } isEmpty() { return this.items.length === 0; } print() { return this.items.join(' -> ') + ' -> null'; } } const pq = new PriorityQueue(); pq.enqueue(4); pq.enqueue(2); pq.enqueue(6); pq.enqueue(1); pq.dequeue(); pq.dequeue(); console.log(pq.print());
Attempts:
2 left
💡 Hint
Dequeue removes the element with the highest priority (lowest number).
✗ Incorrect
After inserting 4, 2, 6, 1, the queue is ordered as 1, 2, 4, 6. Dequeue removes 1, then 2. Remaining queue is 4 -> 6 -> null.
🧠 Conceptual
advancedWhich data structure is best suited to implement a Priority Queue efficiently?
You want to implement a priority queue that supports fast insertion and fast removal of the highest priority element. Which data structure is best suited for this?
Attempts:
2 left
💡 Hint
Think about a structure that keeps partial order and allows quick access to the highest priority.
✗ Incorrect
A binary heap allows insertion and removal of the highest priority element in O(log n) time, making it efficient for priority queues.
❓ Predict Output
advancedWhat error does this Priority Queue code produce?
Consider this code snippet for a priority queue enqueue method. What error will it raise when run?
class PriorityQueue {
constructor() {
this.items = [];
}
enqueue(element) {
for (let i = 0; i < this.items.length; i++) {
if (element < this.items[i])
this.items.splice(i, 0, element);
break;
}
this.items.push(element);
}
}
const pq = new PriorityQueue();
pq.enqueue(3);
DSA Javascript
class PriorityQueue { constructor() { this.items = []; } enqueue(element) { for (let i = 0; i < this.items.length; i++) { if (element < this.items[i]) this.items.splice(i, 0, element); break; } this.items.push(element); } } const pq = new PriorityQueue(); pq.enqueue(3);
Attempts:
2 left
💡 Hint
Check if the loop and if statement syntax is valid.
✗ Incorrect
The code runs without error because JavaScript allows single statements without braces. The queue will contain 3.
🚀 Application
expertHow many items remain after these Priority Queue operations?
A priority queue starts empty. The following operations happen in order:
- enqueue(10)
- enqueue(5)
- enqueue(7)
- dequeue()
- enqueue(3)
- dequeue()
- dequeue()
How many items remain in the queue?
Attempts:
2 left
💡 Hint
Track the queue state step-by-step, remembering dequeue removes the smallest number.
✗ Incorrect
Insertions: 10, 5, 7 → queue: 5,7,10
Dequeue removes 5 → queue: 7,10
Enqueue 3 → queue: 3,7,10
Dequeue removes 3 → queue: 7,10
Dequeue removes 7 → queue: 10
One item remains.
