Challenge - 5 Problems
Stack Pop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output after popping from the stack?
Consider a stack implemented as an array with the following operations. What is the printed stack after one pop operation?
DSA C
int stack[5] = {10, 20, 30, 40, 50}; int top = 4; // points to the last element // Pop operation if(top >= 0) { top--; } // Print stack from bottom to top for(int i = 0; i <= top; i++) { printf("%d ", stack[i]); }
Attempts:
2 left
💡 Hint
Remember, pop removes the top element from the stack.
✗ Incorrect
The top index starts at 4, pointing to 50. After pop, top decreases to 3, so the stack elements from index 0 to 3 are printed: 10 20 30 40.
❓ Predict Output
intermediate2:00remaining
What happens if we pop from an empty stack?
Given the following code, what is the output?
DSA C
int stack[3] = {0}; int top = -1; // empty stack // Pop operation if(top >= 0) { top--; } else { printf("Stack Underflow\n"); }
Attempts:
2 left
💡 Hint
Check the condition before popping.
✗ Incorrect
Since top is -1, the stack is empty. The else block prints 'Stack Underflow'.
🔧 Debug
advanced2:00remaining
Identify the error in this pop operation code
What error will this code produce when popping from the stack?
DSA C
int stack[3] = {1, 2, 3}; int top = 0; // Pop operation if(top >= 0) { top--; } printf("Top element: %d\n", stack[top]);
Attempts:
2 left
💡 Hint
Check the condition for popping carefully.
✗ Incorrect
The condition 'top > 0' prevents popping when top is 0, so pop never happens. The code prints stack[0] which is 1, but the pop operation is incorrect because it should allow popping when top is 0.
❓ Predict Output
advanced2:00remaining
What is the stack content after multiple pops?
Given the stack and operations below, what is the printed stack?
DSA C
int stack[6] = {5, 10, 15, 20, 25, 30}; int top = 5; // Pop twice for(int i = 0; i < 2; i++) { if(top >= 0) { top--; } } // Print stack for(int i = 0; i <= top; i++) { printf("%d ", stack[i]); }
Attempts:
2 left
💡 Hint
Each pop reduces the top index by one.
✗ Incorrect
Starting top=5, after two pops top=3. Stack elements from 0 to 3 are printed: 5 10 15 20.
🧠 Conceptual
expert2:00remaining
What is the time complexity of the pop operation on a stack?
Choose the correct time complexity for the pop operation on a stack implemented using an array.
Attempts:
2 left
💡 Hint
Pop just moves the top pointer.
✗ Incorrect
Pop operation only decrements the top index, which is a constant time operation.
