Challenge - 5 Problems
Stack Using Queue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Push and Pop Operations on Stack Implemented Using One Queue
What is the output of the following C++ code that implements a stack using a single queue and performs push and pop operations?
DSA C++
class Stack { private: std::queue<int> q; public: void push(int x) { int n = q.size(); q.push(x); for (int i = 0; i < n; i++) { q.push(q.front()); q.pop(); } } int pop() { if (q.empty()) return -1; int val = q.front(); q.pop(); return val; } }; int main() { Stack s; s.push(10); s.push(20); s.push(30); int a = s.pop(); int b = s.pop(); return 0; }
Attempts:
2 left
💡 Hint
Remember that the push operation rotates the queue to keep the last pushed element at the front.
✗ Incorrect
The push method rotates the queue so the newly pushed element is always at the front. Thus, pop returns elements in LIFO order: 30 then 20.
❓ Predict Output
intermediateOutput of Push and Pop Operations on Stack Using Two Queues
What is the output of the following C++ code that implements a stack using two queues and performs push and pop operations?
DSA C++
class Stack { private: std::queue<int> q1, q2; public: void push(int x) { q2.push(x); while (!q1.empty()) { q2.push(q1.front()); q1.pop(); } std::swap(q1, q2); } int pop() { if (q1.empty()) return -1; int val = q1.front(); q1.pop(); return val; } }; int main() { Stack s; s.push(5); s.push(15); s.push(25); int a = s.pop(); int b = s.pop(); return 0; }
Attempts:
2 left
💡 Hint
The push operation moves all elements to the second queue to keep the newest element at the front.
✗ Incorrect
The push method ensures the newest element is at the front of q1 by moving all elements from q1 to q2 and swapping. So pop returns 25 then 15.
🔧 Debug
advancedIdentify the Error in Stack Using One Queue Implementation
What error does the following C++ code produce when trying to pop from an empty stack implemented using one queue?
DSA C++
class Stack { private: std::queue<int> q; public: void push(int x) { q.push(x); } int pop() { int n = q.size(); if (n == 0) return -1; for (int i = 0; i < n - 1; i++) { q.push(q.front()); q.pop(); } int val = q.front(); q.pop(); return val; } }; int main() { Stack s; int a = s.pop(); return 0; }
Attempts:
2 left
💡 Hint
Check the condition before popping from the queue.
✗ Incorrect
The pop method checks if the queue is empty and returns -1 safely without throwing an error.
🧠 Conceptual
advancedTime Complexity of Push and Pop in Stack Using One Queue
What is the time complexity of push and pop operations in a stack implemented using a single queue where push rotates the queue?
Attempts:
2 left
💡 Hint
Consider how many elements are moved during push and pop.
✗ Incorrect
Push operation rotates the queue by moving all elements, so it takes O(n). Pop just removes the front element, so O(1).
🚀 Application
expertFinal Stack State After Mixed Operations Using Two Queues
Given the following operations on a stack implemented using two queues, what is the final state of the stack (top to bottom)?
DSA C++
class Stack { private: std::queue<int> q1, q2; public: void push(int x) { q2.push(x); while (!q1.empty()) { q2.push(q1.front()); q1.pop(); } std::swap(q1, q2); } int pop() { if (q1.empty()) return -1; int val = q1.front(); q1.pop(); return val; } std::vector<int> getStack() { std::vector<int> res; std::queue<int> temp = q1; while (!temp.empty()) { res.push_back(temp.front()); temp.pop(); } return res; } }; int main() { Stack s; s.push(1); s.push(2); s.pop(); s.push(3); s.push(4); s.pop(); return 0; }
Attempts:
2 left
💡 Hint
Track the stack after each push and pop operation carefully.
✗ Incorrect
Operations:
- push 1: stack = [1]
- push 2: stack = [2,1]
- pop: removes 2, stack = [1]
- push 3: stack = [3,1]
- push 4: stack = [4,3,1]
- pop: removes 4, stack = [3,1]
Final stack top to bottom is [3,1].
