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 Typescript
class Node { value: number; next: Node | null = null; constructor(value: number) { this.value = value; } } class Queue { front: Node | null = null; rear: Node | null = null; enqueue(value: number) { const newNode = new Node(value); if (!this.rear) { this.front = this.rear = newNode; return; } this.rear.next = newNode; this.rear = newNode; } printQueue() { 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.printQueue();
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, and 30, the queue order is 10 at front, then 20, then 30 at rear.
❓ Predict Output
intermediateWhat happens if we enqueue to an empty queue?
Given the enqueue method below, what will be the state of the queue after enqueueing 5 into an empty queue?
DSA Typescript
class Node { value: number; next: Node | null = null; constructor(value: number) { this.value = value; } } class Queue { front: Node | null = null; rear: Node | null = null; enqueue(value: number) { const newNode = new Node(value); if (!this.rear) { this.front = this.rear = newNode; return; } this.rear.next = newNode; this.rear = newNode; } printQueue() { 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(5); q.printQueue();
Attempts:
2 left
💡 Hint
Check how the code handles the empty queue case.
✗ Incorrect
When the queue is empty, both front and rear point to the new node. So the queue has one element: 5.
🔧 Debug
advancedIdentify the error in this enqueue implementation
What error will this enqueue method cause when adding the first element to an empty queue?
DSA Typescript
enqueue(value: number) {
const newNode = new Node(value);
if (this.front === null) {
this.front.next = newNode;
this.front = newNode;
} else {
this.rear.next = newNode;
this.rear = newNode;
}
}Attempts:
2 left
💡 Hint
Check what happens when front is null and you try to access front.next.
✗ Incorrect
When front is null, front.next causes a TypeError because you cannot access 'next' of null.
❓ Predict Output
advancedWhat is the queue state after multiple enqueues and one dequeue?
Starting from an empty queue, enqueue 1, enqueue 2, enqueue 3, then dequeue once. What is the printed queue state?
DSA Typescript
class Node { value: number; next: Node | null = null; constructor(value: number) { this.value = value; } } class Queue { front: Node | null = null; rear: Node | null = null; enqueue(value: number) { 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; } printQueue() { 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(1); q.enqueue(2); q.enqueue(3); q.dequeue(); q.printQueue();
Attempts:
2 left
💡 Hint
Dequeue removes from the front of the queue.
✗ Incorrect
Dequeue removes the first element (1). Remaining queue is 2 -> 3 -> null.
🧠 Conceptual
expertWhy is it important to update rear when enqueueing in a linked list queue?
In a linked list implementation of a queue, what is the main reason to update the rear pointer when enqueueing a new element?
Attempts:
2 left
💡 Hint
Think about where new elements are added in a queue.
✗ Incorrect
The rear pointer marks the end of the queue so new nodes can be linked there. Without updating rear, enqueue won't add elements correctly.
