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 push and pop sequence?
Consider a stack implemented using an array with a maximum size of 5. The following operations are performed in order:
push(10), push(20), pop(), push(30), push(40), pop(), push(50), push(60), pop()
What is the final state of the stack from bottom to top?
push(10), push(20), pop(), push(30), push(40), pop(), push(50), push(60), pop()
What is the final state of the stack from bottom to top?
DSA C++
class Stack { int arr[5]; int top = -1; public: void push(int x) { if (top == 4) return; // stack full arr[++top] = x; } int pop() { if (top == -1) return -1; // stack empty return arr[top--]; } void print() { for (int i = 0; i <= top; i++) { std::cout << arr[i] << " "; } } }; int main() { Stack s; s.push(10); s.push(20); s.pop(); s.push(30); s.push(40); s.pop(); s.push(50); s.push(60); s.pop(); s.print(); return 0; }
Attempts:
2 left
💡 Hint
Track the stack after each push and pop operation carefully.
✗ Incorrect
After the sequence, the stack contains 10 at bottom, then 30, then 50 at top. Pops remove the last pushed element.
🧠 Conceptual
intermediateWhat happens when you try to push an element into a full stack implemented using an array?
A stack is implemented using an array of fixed size 3. The stack currently has 3 elements. What will happen if you try to push another element?
Attempts:
2 left
💡 Hint
Think about fixed size arrays and their capacity limits.
✗ Incorrect
A fixed size array cannot grow. When full, push operations are rejected or ignored to prevent overflow.
🔧 Debug
advancedWhat error does this stack pop function cause when the stack is empty?
Consider this pop function for a stack implemented using an array:
int pop() { return arr[top--]; }
What happens if pop() is called when the stack is empty (top == -1)?
int pop() { return arr[top--]; }
What happens if pop() is called when the stack is empty (top == -1)?
DSA C++
int pop() { return arr[top--]; }
Attempts:
2 left
💡 Hint
Check what happens when top is -1 and used as an index.
✗ Incorrect
Accessing arr[-1] is invalid and causes undefined behavior or runtime error.
📝 Syntax
advancedWhich option correctly initializes an empty stack using an array in C++?
Choose the correct code snippet that initializes an empty stack with a fixed size array and top pointer.
Attempts:
2 left
💡 Hint
Remember top points to the last filled index or -1 if empty.
✗ Incorrect
Top is -1 when stack is empty because no elements are pushed yet.
🚀 Application
expertAfter performing these operations on a stack implemented with an array, what is the output of the print function?
Given a stack with max size 4, initially empty, perform:
push(5), push(10), push(15), pop(), push(20), push(25), pop(), pop(), push(30)
Then print the stack from bottom to top.
push(5), push(10), push(15), pop(), push(20), push(25), pop(), pop(), push(30)
Then print the stack from bottom to top.
DSA C++
class Stack { int arr[4]; int top = -1; public: void push(int x) { if (top == 3) return; arr[++top] = x; } int pop() { if (top == -1) return -1; return arr[top--]; } void print() { for (int i = 0; i <= top; i++) { std::cout << arr[i] << " "; } } }; int main() { Stack s; s.push(5); s.push(10); s.push(15); s.pop(); s.push(20); s.push(25); s.pop(); s.pop(); s.push(30); s.print(); return 0; }
Attempts:
2 left
💡 Hint
Track each push and pop carefully, updating the stack state.
✗ Incorrect
After all operations, the stack contains 5, 10, 30 from bottom to top.
