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 of this stack operation sequence?
Consider a stack initially empty. We perform these operations in order:
push(10), push(20), pop(), push(30), pop(), push(40), push(50), pop()
What is the content of the stack from top to bottom after these operations?
push(10), push(20), pop(), push(30), pop(), push(40), push(50), pop()
What is the content of the stack from top to bottom after these operations?
DSA C++
std::stack<int> s; s.push(10); s.push(20); s.pop(); s.push(30); s.pop(); s.push(40); s.push(50); s.pop(); // Print stack from top to bottom
Attempts:
2 left
💡 Hint
Remember pop removes the top element. Trace each step carefully.
✗ Incorrect
After the operations, the stack has 40 on top and 10 below. The last pop removed 50, so 40 is top.
🧠 Conceptual
intermediateWhich statement correctly describes the LIFO principle in stacks?
Choose the statement that best explains how the Last In First Out (LIFO) principle works in a stack.
Attempts:
2 left
💡 Hint
Think about which element is on top and removed first.
✗ Incorrect
LIFO means the last element pushed onto the stack is the first to be popped off.
🔧 Debug
advancedWhat error does this stack code produce?
Given the code below, what error will occur when running it?
std::stacks; s.push(1); s.pop(); s.pop();
DSA C++
std::stack<int> s; s.push(1); s.pop(); s.pop();
Attempts:
2 left
💡 Hint
Check what happens if you pop more times than you push.
✗ Incorrect
Popping from an empty stack causes a runtime error (undefined behavior).
🚀 Application
advancedHow many elements remain after this sequence?
A stack starts empty. We perform:
push(5), push(10), push(15), pop(), push(20), pop(), pop()
How many elements are left in the stack?
push(5), push(10), push(15), pop(), push(20), pop(), pop()
How many elements are left in the stack?
Attempts:
2 left
💡 Hint
Count pushes and pops carefully.
✗ Incorrect
After all operations, only one element remains in the stack.
❓ Predict Output
expertWhat is printed by this C++ stack code?
What is the output of this code?
#include <iostream>
#include <stack>
int main() {
std::stack s;
for (int i = 1; i <= 3; ++i) s.push(i);
int sum = 0;
while (!s.empty()) {
sum += s.top();
s.pop();
}
std::cout << sum << std::endl;
return 0;
} DSA C++
#include <iostream> #include <stack> int main() { std::stack<int> s; for (int i = 1; i <= 3; ++i) s.push(i); int sum = 0; while (!s.empty()) { sum += s.top(); s.pop(); } std::cout << sum << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Sum all elements popped from the stack.
✗ Incorrect
Stack contains 1,2,3 pushed. Sum is 1+2+3=6.
