Challenge - 5 Problems
Deque Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this deque operation sequence?
Consider a deque initially empty. We perform these operations in order:
1. Add 10 to the front
2. Add 20 to the back
3. Remove from the front
4. Add 30 to the front
5. Remove from the back
What is the state of the deque after these operations?
1. Add 10 to the front
2. Add 20 to the back
3. Remove from the front
4. Add 30 to the front
5. Remove from the back
What is the state of the deque after these operations?
DSA Javascript
const deque = []; deque.unshift(10); // front deque.push(20); // back deque.shift(); // remove front deque.unshift(30); // front deque.pop(); // remove back console.log(deque);
Attempts:
2 left
💡 Hint
Track each operation step by step on paper or in your mind.
✗ Incorrect
After adding 10 front and 20 back, deque is [10, 20]. Removing front removes 10, leaving [20]. Adding 30 front makes [30, 20]. Removing back removes 20, leaving [30].
🧠 Conceptual
intermediateWhich operation is NOT typical for a double ended queue (deque)?
A deque supports adding and removing elements from both ends. Which of these operations is NOT a standard deque operation?
Attempts:
2 left
💡 Hint
Think about where elements can be added or removed in a deque.
✗ Incorrect
Deques allow adding/removing only at front or back, not in the middle.
❓ Predict Output
advancedWhat is the output after these deque operations?
Given an empty deque, perform:
pushBack(5), pushBack(10), pushFront(3), popBack(), pushFront(7), popFront()
What is the deque content after these?
pushBack(5), pushBack(10), pushFront(3), popBack(), pushFront(7), popFront()
What is the deque content after these?
DSA Javascript
class Deque { constructor() { this.items = []; } pushFront(x) { this.items.unshift(x); } pushBack(x) { this.items.push(x); } popFront() { return this.items.shift(); } popBack() { return this.items.pop(); } } const d = new Deque(); d.pushBack(5); d.pushBack(10); d.pushFront(3); d.popBack(); d.pushFront(7); d.popFront(); console.log(d.items);
Attempts:
2 left
💡 Hint
Write down the deque after each operation.
✗ Incorrect
Operations step by step:
pushBack(5) -> [5]
pushBack(10) -> [5,10]
pushFront(3) -> [3,5,10]
popBack() removes 10 -> [3,5]
pushFront(7) -> [7,3,5]
popFront() removes 7 -> [3,5]
pushBack(5) -> [5]
pushBack(10) -> [5,10]
pushFront(3) -> [3,5,10]
popBack() removes 10 -> [3,5]
pushFront(7) -> [7,3,5]
popFront() removes 7 -> [3,5]
🔧 Debug
advancedWhy does this deque implementation cause an error?
This code tries to implement a deque but throws an error. What is the cause?
DSA Javascript
class Deque { constructor() { this.items = {}; this.frontIndex = 0; this.backIndex = 0; } pushFront(x) { this.frontIndex--; this.items[this.frontIndex] = x; } pushBack(x) { this.items[this.backIndex] = x; this.backIndex++; } popFront() { if (this.frontIndex === this.backIndex) throw new Error('Empty'); const val = this.items[this.frontIndex]; delete this.items[this.frontIndex]; this.frontIndex++; return val; } popBack() { if (this.frontIndex === this.backIndex) throw new Error('Empty'); this.backIndex--; const val = this.items[this.backIndex]; delete this.items[this.backIndex]; return val; } } const d = new Deque(); d.pushBack(1); d.pushFront(2); console.log(d.popFront()); console.log(d.popBack());
Attempts:
2 left
💡 Hint
Think about how JavaScript object keys work with negative numbers.
✗ Incorrect
JavaScript object keys are strings. Negative keys like -1 are valid strings but can cause confusion. The main issue is that frontIndex starts at 0 and is decremented, leading to negative keys which may cause unexpected behavior or errors in some implementations expecting positive indices.
🚀 Application
expertHow many elements remain after this deque operation sequence?
Start with an empty deque. Perform these operations:
pushBack(1), pushBack(2), pushFront(3), popBack(), popFront(), pushBack(4), pushFront(5), popFront(), popBack()
How many elements remain in the deque at the end?
pushBack(1), pushBack(2), pushFront(3), popBack(), popFront(), pushBack(4), pushFront(5), popFront(), popBack()
How many elements remain in the deque at the end?
DSA Javascript
const deque = []; deque.push(1); deque.push(2); deque.unshift(3); deque.pop(); deque.shift(); deque.push(4); deque.unshift(5); deque.shift(); deque.pop(); console.log(deque.length);
Attempts:
2 left
💡 Hint
Count elements after each operation carefully.
✗ Incorrect
Step by step:
pushBack(1) -> [1]
pushBack(2) -> [1,2]
pushFront(3) -> [3,1,2]
popBack() removes 2 -> [3,1]
popFront() removes 3 -> [1]
pushBack(4) -> [1,4]
pushFront(5) -> [5,1,4]
popFront() removes 5 -> [1,4]
popBack() removes 4 -> [1]
Final deque has 1 element.
pushBack(1) -> [1]
pushBack(2) -> [1,2]
pushFront(3) -> [3,1,2]
popBack() removes 2 -> [3,1]
popFront() removes 3 -> [1]
pushBack(4) -> [1,4]
pushFront(5) -> [5,1,4]
popFront() removes 5 -> [1,4]
popBack() removes 4 -> [1]
Final deque has 1 element.
