Challenge - 5 Problems
Stack-Queue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after these stack operations?
Consider a stack implemented using a single queue. The stack supports push and pop operations. What will be the printed stack state after the following operations?
push(1), push(2), push(3), pop(), push(4)
push(1), push(2), push(3), pop(), push(4)
DSA Javascript
class StackUsingQueue { constructor() { this.queue = []; } push(x) { this.queue.push(x); let size = this.queue.length; while (size > 1) { this.queue.push(this.queue.shift()); size--; } } pop() { return this.queue.shift(); } print() { console.log(this.queue.join(' -> ') + ' -> null'); } } const stack = new StackUsingQueue(); stack.push(1); stack.push(2); stack.push(3); stack.pop(); stack.push(4); stack.print();
Attempts:
2 left
💡 Hint
Remember that push rotates the queue to keep the last pushed element at the front.
✗ Incorrect
The push operation rotates the queue so the last pushed element is always at the front. After pushing 1, 2, 3, the queue is [3, 2, 1]. Popping removes 3. Then pushing 4 rotates the queue to [4, 2, 1].
🧠 Conceptual
intermediateWhy does the push operation rotate the queue in stack implementation?
In a stack implemented using a single queue, the push operation rotates the queue after adding the new element. Why is this rotation necessary?
Attempts:
2 left
💡 Hint
Think about how a stack pops the last pushed element.
✗ Incorrect
The rotation moves the newly pushed element to the front of the queue so that pop (which removes from front) behaves like stack pop (LIFO).
❓ Predict Output
advancedWhat error occurs in this stack pop implementation?
Given this stack implemented with two queues, what error or output occurs when pop is called on an empty stack?
DSA Javascript
class StackUsingTwoQueues { constructor() { this.q1 = []; this.q2 = []; } push(x) { this.q1.push(x); } pop() { if (this.q1.length === 0) { return this.q1.shift(); } while (this.q1.length > 1) { this.q2.push(this.q1.shift()); } const popped = this.q1.shift(); [this.q1, this.q2] = [this.q2, this.q1]; return popped; } } const stack = new StackUsingTwoQueues(); console.log(stack.pop());
Attempts:
2 left
💡 Hint
What happens when shift is called on an empty array?
✗ Incorrect
Calling shift on an empty array returns undefined without throwing an error.
❓ Predict Output
advancedWhat is the final queue state after these operations?
Using two queues to implement a stack, what is the final state of q1 after these operations?
push(10), push(20), pop(), push(30), pop()
push(10), push(20), pop(), push(30), pop()
DSA Javascript
class StackUsingTwoQueues { constructor() { this.q1 = []; this.q2 = []; } push(x) { this.q1.push(x); } pop() { while (this.q1.length > 1) { this.q2.push(this.q1.shift()); } const popped = this.q1.shift(); [this.q1, this.q2] = [this.q2, this.q1]; return popped; } } const stack = new StackUsingTwoQueues(); stack.push(10); stack.push(20); stack.pop(); stack.push(30); stack.pop(); console.log(stack.q1);
Attempts:
2 left
💡 Hint
Trace each operation carefully and watch how queues swap.
✗ Incorrect
After first pop, 20 is removed, q1 becomes [10]. Then push 30 adds to q1: [10,30]. Second pop removes 30, leaving q1 as [10].
🧠 Conceptual
expertWhat is the time complexity of push and pop in stack using single queue?
In a stack implemented using a single queue where push rotates the queue, what are the time complexities of push and pop operations respectively?
Attempts:
2 left
💡 Hint
Consider how many elements are moved during push and pop.
✗ Incorrect
Push rotates the queue by moving all elements, so O(n). Pop just removes from front, O(1).
