Challenge - 5 Problems
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after pushing elements on the stack?
Consider the following C++ code that pushes elements onto a stack. What will be the printed stack content after all push operations?
DSA C++
std::stack<int> s; s.push(10); s.push(20); s.push(30); while(!s.empty()) { std::cout << s.top() << " -> "; s.pop(); } std::cout << "null";
Attempts:
2 left
💡 Hint
Remember that stack is Last-In-First-Out (LIFO). The last pushed element is printed first.
✗ Incorrect
The stack pushes 10, then 20, then 30. The top is 30, so popping prints 30 first, then 20, then 10.
🧠 Conceptual
intermediateWhat happens internally when you push an element on a stack?
When you perform a push operation on a stack, which of the following best describes what happens internally?
Attempts:
2 left
💡 Hint
Think about where the new element appears relative to existing elements.
✗ Incorrect
Push adds the new element on top of the stack without removing or rearranging existing elements.
🔧 Debug
advancedIdentify the error in this push operation code
What error will this C++ code produce when trying to push elements onto a stack?
DSA C++
std::stack<int> s; s.push(5); s.pop(); s.top(); s.push(10); std::cout << s.top();
Attempts:
2 left
💡 Hint
Check the order of pop and top calls and whether the stack is empty.
✗ Incorrect
After popping the only element, the stack is empty. Calling top() then causes a runtime error.
❓ Predict Output
advancedWhat is the stack content after these push and pop operations?
Given the following C++ code, what will be the printed stack content?
DSA C++
std::stack<int> s; s.push(1); s.push(2); s.pop(); s.push(3); while(!s.empty()) { std::cout << s.top() << " -> "; s.pop(); } std::cout << "null";
Attempts:
2 left
💡 Hint
Remember pop removes the top element before the next push.
✗ Incorrect
Push 1, push 2, pop removes 2, push 3. Stack top is 3, then 1.
🚀 Application
expertHow many elements are in the stack after this sequence?
If you start with an empty stack and perform the following operations: push(4), push(7), pop(), push(9), pop(), pop(), push(5), how many elements remain in the stack?
Attempts:
2 left
💡 Hint
Track each operation step-by-step to count remaining elements.
✗ Incorrect
Operations: push 4 (stack:4), push 7 (4,7), pop removes 7 (4), push 9 (4,9), pop removes 9 (4), pop removes 4 (empty), push 5 (5). One element remains.
