Challenge - 5 Problems
Dequeue Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Dequeue Operations
Consider a dequeue implemented using a linked list. The following operations are performed in order:
1. Insert 10 at rear
2. Insert 20 at front
3. Insert 30 at rear
4. Delete from front
5. Insert 40 at front
6. Delete from rear
What is the printed state of the dequeue after these operations?
1. Insert 10 at rear
2. Insert 20 at front
3. Insert 30 at rear
4. Delete from front
5. Insert 40 at front
6. Delete from rear
What is the printed state of the dequeue after these operations?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; this.prev = null; } } class Dequeue { constructor() { this.front = null; this.rear = null; } insertFront(data) { const newNode = new Node(data); if (!this.front) { this.front = this.rear = newNode; } else { newNode.next = this.front; this.front.prev = newNode; this.front = newNode; } } insertRear(data) { const newNode = new Node(data); if (!this.rear) { this.front = this.rear = newNode; } else { this.rear.next = newNode; newNode.prev = this.rear; this.rear = newNode; } } deleteFront() { if (!this.front) return null; const val = this.front.data; if (this.front === this.rear) { this.front = this.rear = null; } else { this.front = this.front.next; this.front.prev = null; } return val; } deleteRear() { if (!this.rear) return null; const val = this.rear.data; if (this.front === this.rear) { this.front = this.rear = null; } else { this.rear = this.rear.prev; this.rear.next = null; } return val; } print() { let curr = this.front; let result = ''; while (curr) { result += curr.data + ' -> '; curr = curr.next; } return result + 'null'; } } const dq = new Dequeue(); dq.insertRear(10); dq.insertFront(20); dq.insertRear(30); dq.deleteFront(); dq.insertFront(40); dq.deleteRear(); console.log(dq.print());
Attempts:
2 left
💡 Hint
Trace each operation step-by-step and update the linked list accordingly.
✗ Incorrect
After all operations, the dequeue contains 40 at front and 10 at rear. The sequence is 40 -> 10 -> null.
🧠 Conceptual
intermediateUnderstanding Dequeue Front and Rear Pointers
In a dequeue implemented using a doubly linked list, which statement is TRUE about the front and rear pointers after deleting the last element?
Attempts:
2 left
💡 Hint
Think about what happens when the dequeue becomes empty.
✗ Incorrect
When the last element is deleted, the dequeue is empty, so both front and rear pointers must be null.
🔧 Debug
advancedIdentify the Bug in Dequeue Rear Deletion
What error will occur when running the following code snippet for deleting from rear in a dequeue implemented with a doubly linked list?
DSA Javascript
deleteRear() {
if (!this.rear) return null;
const val = this.rear.data;
if (this.front === this.rear) {
this.front = this.rear = null;
} else {
this.rear = this.rear.prev;
this.rear.next = null;
}
return val;
}
// Assume dequeue has only one element before calling deleteRear()Attempts:
2 left
💡 Hint
Check the condition when front and rear are the same node.
✗ Incorrect
The code correctly handles the case when there is only one element by setting front and rear to null, so no error occurs.
❓ Predict Output
advancedResult of Mixed Insertions and Deletions
Given a dequeue implemented with a doubly linked list, what is the output after these operations?
insertFront(5)
insertRear(15)
insertFront(3)
deleteRear()
insertRear(20)
deleteFront()
print()
insertFront(5)
insertRear(15)
insertFront(3)
deleteRear()
insertRear(20)
deleteFront()
print()
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; this.prev = null; } } class Dequeue { constructor() { this.front = null; this.rear = null; } insertFront(data) { const newNode = new Node(data); if (!this.front) { this.front = this.rear = newNode; } else { newNode.next = this.front; this.front.prev = newNode; this.front = newNode; } } insertRear(data) { const newNode = new Node(data); if (!this.rear) { this.front = this.rear = newNode; } else { this.rear.next = newNode; newNode.prev = this.rear; this.rear = newNode; } } deleteFront() { if (!this.front) return null; const val = this.front.data; if (this.front === this.rear) { this.front = this.rear = null; } else { this.front = this.front.next; this.front.prev = null; } return val; } deleteRear() { if (!this.rear) return null; const val = this.rear.data; if (this.front === this.rear) { this.front = this.rear = null; } else { this.rear = this.rear.prev; this.rear.next = null; } return val; } print() { let curr = this.front; let result = ''; while (curr) { result += curr.data + ' -> '; curr = curr.next; } return result + 'null'; } } const dq = new Dequeue(); dq.insertFront(5); dq.insertRear(15); dq.insertFront(3); dq.deleteRear(); dq.insertRear(20); dq.deleteFront(); console.log(dq.print());
Attempts:
2 left
💡 Hint
Track the list after each operation carefully.
✗ Incorrect
After all operations, the dequeue contains 5 at front and 20 at rear, so the output is '5 -> 20 -> null'.
🧠 Conceptual
expertTime Complexity of Dequeue Operations Using Linked List
What is the time complexity of insertions and deletions at both ends (front and rear) in a dequeue implemented using a doubly linked list?
Attempts:
2 left
💡 Hint
Consider how pointers are updated in a doubly linked list.
✗ Incorrect
Using a doubly linked list with front and rear pointers allows all insertions and deletions at both ends in constant time O(1).
