Challenge - 5 Problems
Queue Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after enqueue and dequeue operations?
Consider a queue implemented using a linked list. Initially, the queue is empty. We perform the following operations:
enqueue(10), enqueue(20), dequeue(), enqueue(30), dequeue()
What is the state of the queue after these operations?
enqueue(10), enqueue(20), dequeue(), enqueue(30), dequeue()
What is the state of the queue after these operations?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class Queue { constructor() { this.front = null; this.rear = null; } enqueue(value) { const newNode = new Node(value); if (!this.rear) { this.front = this.rear = newNode; return; } this.rear.next = newNode; this.rear = newNode; } dequeue() { if (!this.front) return null; const temp = this.front; this.front = this.front.next; if (!this.front) this.rear = null; return temp.value; } printQueue() { let current = this.front; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } return result + 'null'; } } const q = new Queue(); q.enqueue(10); q.enqueue(20); q.dequeue(); q.enqueue(30); q.dequeue(); console.log(q.printQueue());
Attempts:
2 left
💡 Hint
Trace each operation step-by-step and see which nodes remain in the queue.
✗ Incorrect
After enqueue(10) and enqueue(20), queue is 10 -> 20 -> null. dequeue() removes 10, queue becomes 20 -> null. enqueue(30) adds 30 at rear, queue is 20 -> 30 -> null. dequeue() removes 20, queue becomes 30 -> null.
🧠 Conceptual
intermediateWhat happens when dequeue is called on an empty queue?
In a queue implemented using a linked list, what is the expected behavior when the dequeue operation is called but the queue is empty?
Attempts:
2 left
💡 Hint
Think about safe handling of empty data structures.
✗ Incorrect
When dequeue is called on an empty queue, it should safely return null or a similar value to indicate no elements are available, avoiding errors.
🔧 Debug
advancedIdentify the bug in this enqueue method
The following enqueue method is intended to add a new node to the rear of the queue. What is the bug that will cause incorrect behavior?
DSA Javascript
enqueue(value) {
const newNode = new Node(value);
if (!this.front) {
this.front = newNode;
}
this.rear.next = newNode;
this.rear = newNode;
}Attempts:
2 left
💡 Hint
Consider what happens when the queue is empty and rear is null.
✗ Incorrect
When the queue is empty, rear is null, so accessing rear.next causes an error. The code should set both front and rear to newNode in this case.
❓ Predict Output
advancedWhat is the output after multiple enqueue and dequeue?
Given the following operations on an empty queue:
enqueue(5), enqueue(15), enqueue(25), dequeue(), enqueue(35), dequeue(), dequeue()
What is the printed state of the queue?
enqueue(5), enqueue(15), enqueue(25), dequeue(), enqueue(35), dequeue(), dequeue()
What is the printed state of the queue?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class Queue { constructor() { this.front = null; this.rear = null; } enqueue(value) { const newNode = new Node(value); if (!this.rear) { this.front = this.rear = newNode; return; } this.rear.next = newNode; this.rear = newNode; } dequeue() { if (!this.front) return null; const temp = this.front; this.front = this.front.next; if (!this.front) this.rear = null; return temp.value; } printQueue() { let current = this.front; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } return result + 'null'; } } const q = new Queue(); q.enqueue(5); q.enqueue(15); q.enqueue(25); q.dequeue(); q.enqueue(35); q.dequeue(); q.dequeue(); console.log(q.printQueue());
Attempts:
2 left
💡 Hint
Track each enqueue and dequeue carefully to see which nodes remain.
✗ Incorrect
Operations:
enqueue(5), enqueue(15), enqueue(25) → queue: 5 -> 15 -> 25 -> null
dequeue() removes 5 → queue: 15 -> 25 -> null
enqueue(35) → queue: 15 -> 25 -> 35 -> null
dequeue() removes 15 → queue: 25 -> 35 -> null
dequeue() removes 25 → queue: 35 -> null
Final queue: 35 -> null
enqueue(5), enqueue(15), enqueue(25) → queue: 5 -> 15 -> 25 -> null
dequeue() removes 5 → queue: 15 -> 25 -> null
enqueue(35) → queue: 15 -> 25 -> 35 -> null
dequeue() removes 15 → queue: 25 -> 35 -> null
dequeue() removes 25 → queue: 35 -> null
Final queue: 35 -> null
🧠 Conceptual
expertWhy is a linked list preferred over an array for queue implementation in some cases?
Which of the following is the main reason to use a linked list instead of an array to implement a queue?
Attempts:
2 left
💡 Hint
Think about what happens when you remove the first element from an array.
✗ Incorrect
In arrays, dequeue requires shifting all elements which takes linear time. Linked lists allow adding/removing nodes at ends in constant time without shifting.
