Challenge - 5 Problems
Queue Two Stacks Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of enqueue and dequeue operations
What is the printed state of the queue after the following operations using two stacks?
DSA Javascript
class QueueUsingStacks { constructor() { this.stack1 = []; this.stack2 = []; } enqueue(val) { this.stack1.push(val); } dequeue() { if (this.stack2.length === 0) { while (this.stack1.length > 0) { this.stack2.push(this.stack1.pop()); } } return this.stack2.pop(); } printQueue() { const combined = [...this.stack2.slice().reverse(), ...this.stack1]; console.log(combined.join(' -> ') + ' -> null'); } } const q = new QueueUsingStacks(); q.enqueue(1); q.enqueue(2); q.enqueue(3); q.dequeue(); q.enqueue(4); q.printQueue();
Attempts:
2 left
💡 Hint
Remember that dequeue removes the oldest element, and elements are moved between stacks to maintain order.
✗ Incorrect
After enqueueing 1, 2, 3, the queue is 1 -> 2 -> 3. Dequeue removes 1. Then enqueue 4 adds it to the end. The queue is now 2 -> 3 -> 4.
❓ Predict Output
intermediateResult after multiple dequeues
What will be the output of the queue after these operations?
DSA Javascript
const q = new (class { constructor() { this.s1 = []; this.s2 = []; } enqueue(x) { this.s1.push(x); } dequeue() { if (this.s2.length === 0) { while (this.s1.length) this.s2.push(this.s1.pop()); } return this.s2.pop(); } print() { const arr = [...this.s2.slice().reverse(), ...this.s1]; console.log(arr.join(' -> ') + ' -> null'); } })(); q.enqueue(10); q.enqueue(20); q.enqueue(30); q.dequeue(); q.dequeue(); q.print();
Attempts:
2 left
💡 Hint
Two dequeues remove the first two enqueued elements.
✗ Incorrect
Enqueued 10, 20, 30. Dequeue removes 10, then 20. Remaining queue is 30.
🔧 Debug
advancedIdentify the error in dequeue method
What error will this code raise when calling dequeue on an empty queue?
DSA Javascript
class QueueTwoStacks { constructor() { this.inStack = []; this.outStack = []; } enqueue(val) { this.inStack.push(val); } dequeue() { if (this.outStack.length === 0) { while (this.inStack.length > 0) { this.outStack.push(this.inStack.pop()); } } return this.outStack.pop(); } } const q = new QueueTwoStacks(); q.dequeue();
Attempts:
2 left
💡 Hint
What happens when pop is called on an empty array?
✗ Incorrect
Calling pop on an empty array returns undefined, so dequeue returns undefined without error.
❓ Predict Output
advancedQueue state after interleaved operations
What is the queue state after these operations?
DSA Javascript
const q = new (class { constructor() { this.s1 = []; this.s2 = []; } enqueue(x) { this.s1.push(x); } dequeue() { if (this.s2.length === 0) { while (this.s1.length) this.s2.push(this.s1.pop()); } return this.s2.pop(); } print() { const arr = [...this.s2.slice().reverse(), ...this.s1]; console.log(arr.join(' -> ') + ' -> null'); } })(); q.enqueue(5); q.enqueue(6); q.dequeue(); q.enqueue(7); q.dequeue(); q.enqueue(8); q.print();
Attempts:
2 left
💡 Hint
Track each enqueue and dequeue carefully, noting when stacks transfer elements.
✗ Incorrect
Enqueue 5,6; dequeue removes 5; enqueue 7; dequeue removes 6; enqueue 8; queue is 7 -> 8.
🧠 Conceptual
expertTime complexity of dequeue operation
What is the amortized time complexity of the dequeue operation in a queue implemented using two stacks?
Attempts:
2 left
💡 Hint
Consider how often elements are moved between stacks.
✗ Incorrect
Each element is moved at most twice (push and pop), so amortized dequeue is O(1).
