Challenge - 5 Problems
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output after pushing elements on the stack?
Consider a stack implemented using an array with a maximum size of 5. Initially, the stack is empty. We perform the following push operations in order: push(10), push(20), push(30). What is the printed state of the stack after these operations?
DSA C
int stack[5]; int top = -1; void push(int x) { if (top == 4) { printf("Stack Overflow\n"); return; } stack[++top] = x; } void printStack() { for (int i = 0; i <= top; i++) { printf("%d -> ", stack[i]); } printf("null\n"); } int main() { push(10); push(20); push(30); printStack(); return 0; }
Attempts:
2 left
💡 Hint
Remember that push adds elements on top, and printStack prints from bottom to top index.
✗ Incorrect
The push function adds elements at the next top index. After pushing 10, 20, and 30, the stack array holds [10, 20, 30] from index 0 to 2. The printStack function prints from index 0 to top, so the output is 10 -> 20 -> 30 -> null.
❓ Predict Output
intermediate2:00remaining
What happens when pushing beyond stack capacity?
Given a stack with maximum size 3, initially empty, we push elements 5, 15, 25, and then try to push 35. What will be the output after these operations?
DSA C
int stack[3]; int top = -1; void push(int x) { if (top == 2) { printf("Stack Overflow\n"); return; } stack[++top] = x; } void printStack() { for (int i = 0; i <= top; i++) { printf("%d -> ", stack[i]); } printf("null\n"); } int main() { push(5); push(15); push(25); push(35); printStack(); return 0; }
Attempts:
2 left
💡 Hint
Check the condition that prevents pushing when stack is full.
✗ Incorrect
The stack size is 3, so pushing the fourth element triggers 'Stack Overflow' message. The stack remains with the first three elements. Printing shows '5 -> 15 -> 25 -> null'.
🧠 Conceptual
advanced1:30remaining
How does the push operation affect the 'top' pointer in a stack?
In a stack implemented using an array, what is the correct way the 'top' pointer changes when an element is pushed?
Attempts:
2 left
💡 Hint
Think about where the new element should be placed relative to the current top.
✗ Incorrect
When pushing, we first move the 'top' pointer up by one to point to the new empty slot, then place the new element there. This ensures the top always points to the last inserted element.
🔧 Debug
advanced2:00remaining
Identify the error in this push function implementation
What error will occur when running this push function on a stack of size 5?
DSA C
int stack[5]; int top = -1; void push(int x) { if (top == 4) { printf("Stack Overflow\n"); return; } stack[++top] = x; } int main() { push(1); push(2); push(3); return 0; }
Attempts:
2 left
💡 Hint
Check how 'top' is used and updated in the push function.
✗ Incorrect
The condition should be 'top == 4' to prevent overflow since array indices go from 0 to 4. Also, 'stack[top++] = x;' uses post-increment, so the first assignment is at stack[-1], which is invalid and causes memory corruption.
🚀 Application
expert3:00remaining
After a sequence of push and pop operations, what is the stack state?
A stack with max size 4 is initially empty. The following operations are performed: push(7), push(14), pop(), push(21), push(28), pop(), push(35). What is the printed stack state after these operations?
DSA C
int stack[4]; int top = -1; void push(int x) { if (top == 3) { printf("Stack Overflow\n"); return; } stack[++top] = x; } void pop() { if (top == -1) { printf("Stack Underflow\n"); return; } top--; } void printStack() { for (int i = 0; i <= top; i++) { printf("%d -> ", stack[i]); } printf("null\n"); } int main() { push(7); push(14); pop(); push(21); push(28); pop(); push(35); printStack(); return 0; }
Attempts:
2 left
💡 Hint
Track the top index carefully after each push and pop.
✗ Incorrect
Operations:
- push(7): stack=[7], top=0
- push(14): stack=[7,14], top=1
- pop(): removes 14, top=0
- push(21): stack=[7,21], top=1
- push(28): stack=[7,21,28], top=2
- pop(): removes 28, top=1
- push(35): stack=[7,21,35], top=2
Printing from 0 to 2 gives '7 -> 21 -> 35 -> null'.
