Challenge - 5 Problems
Linked List Queue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after enqueue operations?
Consider a linked list queue initially empty. We enqueue 10, then 20, then 30. What is the printed 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; } print() { let current = this.front; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const q = new Queue(); q.enqueue(10); q.enqueue(20); q.enqueue(30); q.print();
Attempts:
2 left
💡 Hint
Remember, enqueue adds to the rear of the queue.
✗ Incorrect
Enqueue adds elements at the rear. So after adding 10, 20, 30, the queue order is 10 at front, then 20, then 30 at rear.
❓ Predict Output
intermediateWhat error occurs with incorrect enqueue code?
What error will this enqueue method cause when called on an empty queue?
enqueue(value) {
const newNode = new Node(value);
this.rear.next = newNode;
this.rear = newNode;
if (!this.front) {
this.front = newNode;
}
}
Attempts:
2 left
💡 Hint
Check what happens when rear is null and you try to access rear.next.
✗ Incorrect
If the queue is empty, rear is null. Trying to access rear.next causes a TypeError because you can't access 'next' of null.
🧠 Conceptual
advancedWhy do we update rear pointer in enqueue?
In a linked list implementation of a queue, why is it necessary to update the rear pointer when enqueueing a new node?
Attempts:
2 left
💡 Hint
Think about where new nodes are added in a queue.
✗ Incorrect
The rear pointer always points to the last node so we can add new nodes quickly at the end without traversing the whole list.
❓ Predict Output
advancedWhat is the queue state after mixed enqueue and dequeue?
Given this queue code, what is printed after these operations?
q.enqueue(5);
q.enqueue(15);
q.dequeue();
q.enqueue(25);
q.print();
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 value = this.front.value; this.front = this.front.next; if (!this.front) this.rear = null; return value; } print() { let current = this.front; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const q = new Queue();
Attempts:
2 left
💡 Hint
Dequeue removes from front, enqueue adds at rear.
✗ Incorrect
After enqueue 5 and 15, queue is 5 -> 15 -> null. Dequeue removes 5. Then enqueue 25 adds at rear. Final queue: 15 -> 25 -> null.
🧠 Conceptual
expertWhat is the time complexity of enqueue in linked list queue?
What is the time complexity of the enqueue operation in a linked list implementation of a queue when we maintain both front and rear pointers?
Attempts:
2 left
💡 Hint
Think about how rear pointer helps insertion.
✗ Incorrect
With rear pointer, enqueue adds new node directly at the end in constant time without traversal.
