Challenge - 5 Problems
Dequeue Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after multiple enqueue and dequeue operations
What is the state of the dequeue after executing the following operations in order?
DSA Typescript
class Dequeue { items: number[] = []; enqueueFront(element: number) { this.items.unshift(element); } enqueueRear(element: number) { this.items.push(element); } dequeueFront(): number | undefined { return this.items.shift(); } dequeueRear(): number | undefined { return this.items.pop(); } print(): string { return this.items.join(' -> ') + ' -> null'; } } const dq = new Dequeue(); dq.enqueueRear(10); dq.enqueueRear(20); dq.enqueueFront(5); dq.dequeueRear(); dq.enqueueFront(3); dq.dequeueFront(); console.log(dq.print());
Attempts:
2 left
💡 Hint
Trace each operation step-by-step, updating the dequeue contents.
✗ Incorrect
Step-by-step:
- enqueueRear(10): [10]
- enqueueRear(20): [10, 20]
- enqueueFront(5): [5, 10, 20]
- dequeueRear(): removes 20 → [5, 10]
- enqueueFront(3): [3, 5, 10]
- dequeueFront(): removes 3 → [5, 10]
Final state: 5 -> 10 -> null
❓ Predict Output
intermediateResult after dequeueFront on empty dequeue
What will be the output of the following code snippet?
DSA Typescript
class Dequeue { items: number[] = []; dequeueFront(): number | undefined { return this.items.shift(); } } const dq = new Dequeue(); const result = dq.dequeueFront(); console.log(result);
Attempts:
2 left
💡 Hint
What does shift() return when called on an empty array?
✗ Incorrect
The shift() method returns undefined if the array is empty. It does not throw an error or return null.
❓ Predict Output
advancedOutput after complex dequeue operations
What is the printed output after running this code?
DSA Typescript
class Dequeue { items: number[] = []; enqueueFront(element: number) { this.items.unshift(element); } enqueueRear(element: number) { this.items.push(element); } dequeueFront(): number | undefined { return this.items.shift(); } dequeueRear(): number | undefined { return this.items.pop(); } print(): string { return this.items.join(' -> ') + ' -> null'; } } const dq = new Dequeue(); dq.enqueueRear(1); dq.enqueueRear(2); dq.enqueueFront(3); dq.enqueueRear(4); dq.dequeueFront(); dq.dequeueRear(); dq.enqueueFront(5); console.log(dq.print());
Attempts:
2 left
💡 Hint
Follow each enqueue and dequeue carefully, updating the list.
✗ Incorrect
Operations step-by-step:
- enqueueRear(1): [1]
- enqueueRear(2): [1, 2]
- enqueueFront(3): [3, 1, 2]
- enqueueRear(4): [3, 1, 2, 4]
- dequeueFront(): removes 3 → [1, 2, 4]
- dequeueRear(): removes 4 → [1, 2]
- enqueueFront(5): [5, 1, 2]
Final state: 5 -> 1 -> 2 -> null
🧠 Conceptual
advancedUnderstanding time complexity of dequeue operations
Which statement correctly describes the time complexity of enqueueFront and dequeueRear operations in a dequeue implemented with a doubly linked list?
Attempts:
2 left
💡 Hint
Think about how doubly linked lists store references to both ends.
✗ Incorrect
In a doubly linked list, both front and rear nodes are directly accessible. Adding or removing nodes at either end is done by updating pointers, which takes constant time O(1).
🔧 Debug
expertIdentify the bug causing incorrect dequeueRear behavior
Consider this dequeue implementation. Which option correctly identifies the bug causing dequeueRear to remove from the front instead of the rear?
DSA Typescript
class Dequeue { items: number[] = []; enqueueRear(element: number) { this.items.push(element); } dequeueRear(): number | undefined { if (this.items.length === 0) { return undefined; } return this.items.shift(); } } const dq = new Dequeue(); dq.enqueueRear(10); dq.enqueueRear(20); console.log(dq.dequeueRear());
Attempts:
2 left
💡 Hint
Check which method removes from the front and which from the rear in arrays.
✗ Incorrect
The bug is that dequeueRear uses shift() which removes the first element (front), but it should use pop() to remove the last element (rear).
