Challenge - 5 Problems
Dequeue Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Dequeue Operations
What is the printed state of the dequeue after performing these operations?
Operations:
1. Add 10 at rear
2. Add 20 at front
3. Add 30 at rear
4. Remove from front
5. Add 40 at front
6. Remove from rear
Operations:
1. Add 10 at rear
2. Add 20 at front
3. Add 30 at rear
4. Remove from front
5. Add 40 at front
6. Remove from rear
DSA Typescript
class Node { value: number; next: Node | null = null; prev: Node | null = null; constructor(value: number) { this.value = value; } } class Dequeue { front: Node | null = null; rear: Node | null = null; addFront(value: number) { const newNode = new Node(value); if (!this.front) { this.front = this.rear = newNode; } else { newNode.next = this.front; this.front.prev = newNode; this.front = newNode; } } addRear(value: number) { const newNode = new Node(value); if (!this.rear) { this.front = this.rear = newNode; } else { newNode.prev = this.rear; this.rear.next = newNode; this.rear = newNode; } } removeFront() { if (!this.front) return null; const val = this.front.value; this.front = this.front.next; if (this.front) this.front.prev = null; else this.rear = null; return val; } removeRear() { if (!this.rear) return null; const val = this.rear.value; this.rear = this.rear.prev; if (this.rear) this.rear.next = null; else this.front = null; return val; } print() { let curr = this.front; const values = []; while (curr) { values.push(curr.value); curr = curr.next; } console.log(values.join(' -> ') + ' -> null'); } } const dq = new Dequeue(); dq.addRear(10); dq.addFront(20); dq.addRear(30); dq.removeFront(); dq.addFront(40); dq.removeRear(); dq.print();
Attempts:
2 left
💡 Hint
Trace each operation step-by-step and update the dequeue state.
✗ Incorrect
After all operations, the dequeue contains 40 at front and 10 at rear, so the printed state is '40 -> 10 -> null'.
🧠 Conceptual
intermediateUnderstanding Dequeue Front and Rear Pointers
In a dequeue implemented using a doubly linked list, what happens to the front and rear pointers when the last element is removed?
Attempts:
2 left
💡 Hint
Think about what the dequeue looks like when empty.
✗ Incorrect
When the last element is removed, the dequeue becomes empty, so both front and rear pointers must be set to null to indicate no elements.
🔧 Debug
advancedIdentify the Bug in removeRear Method
What error will occur when running this removeRear method if the dequeue has only one element?
DSA Typescript
removeRear() {
if (!this.rear) return null;
const val = this.rear.value;
this.rear = this.rear.prev;
this.rear.next = null;
return val;
}Attempts:
2 left
💡 Hint
Consider what happens when this.rear.prev is null.
✗ Incorrect
If the dequeue has one element, this.rear.prev is null, so setting this.rear.next causes a TypeError because this.rear is null.
❓ Predict Output
advancedResulting Dequeue After Mixed Operations
What is the printed state of the dequeue after these operations?
Operations:
addFront(5), addRear(15), addFront(25), removeRear(), addRear(35), removeFront()
Operations:
addFront(5), addRear(15), addFront(25), removeRear(), addRear(35), removeFront()
DSA Typescript
class Node { value: number; next: Node | null = null; prev: Node | null = null; constructor(value: number) { this.value = value; } } class Dequeue { front: Node | null = null; rear: Node | null = null; addFront(value: number) { const newNode = new Node(value); if (!this.front) { this.front = this.rear = newNode; } else { newNode.next = this.front; this.front.prev = newNode; this.front = newNode; } } addRear(value: number) { const newNode = new Node(value); if (!this.rear) { this.front = this.rear = newNode; } else { newNode.prev = this.rear; this.rear.next = newNode; this.rear = newNode; } } removeFront() { if (!this.front) return null; const val = this.front.value; this.front = this.front.next; if (this.front) this.front.prev = null; else this.rear = null; return val; } removeRear() { if (!this.rear) return null; const val = this.rear.value; this.rear = this.rear.prev; if (this.rear) this.rear.next = null; else this.front = null; return val; } print() { let curr = this.front; const values = []; while (curr) { values.push(curr.value); curr = curr.next; } console.log(values.join(' -> ') + ' -> null'); } } const dq = new Dequeue(); dq.addFront(5); dq.addRear(15); dq.addFront(25); dq.removeRear(); dq.addRear(35); dq.removeFront(); dq.print();
Attempts:
2 left
💡 Hint
Track the dequeue state after each operation carefully.
✗ Incorrect
After all operations, the dequeue contains 5 at front and 35 at rear, so the printed state is '5 -> 35 -> null'.
🧠 Conceptual
expertTime Complexity of Dequeue Operations Using Linked List
What is the time complexity of adding or removing elements from both front and rear in a dequeue implemented with a doubly linked list?
Attempts:
2 left
💡 Hint
Consider how pointers are updated in a doubly linked list.
✗ Incorrect
All add and remove operations at front and rear are done by updating pointers directly, so they take constant time O(1).
