Challenge - 5 Problems
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Stack Empty Check
What is the output of this C code that checks if the stack is empty after some push and pop operations?
DSA C
#include <stdio.h> #define MAX 3 int stack[MAX]; int top = -1; int isEmpty() { return top == -1; } void push(int val) { if (top < MAX - 1) { stack[++top] = val; } } void pop() { if (!isEmpty()) { top--; } } int main() { push(10); push(20); pop(); pop(); if (isEmpty()) { printf("Stack is empty\n"); } else { printf("Stack is not empty\n"); } return 0; }
Attempts:
2 left
💡 Hint
Think about how many elements are pushed and popped before the check.
✗ Incorrect
After pushing 10 and 20, the stack has two elements. Then two pops remove both elements, making the stack empty. So isEmpty() returns true and prints 'Stack is empty'.
❓ Predict Output
intermediate2:00remaining
Output of Stack Full Check
What is the output of this C code that checks if the stack is full after pushing elements?
DSA C
#include <stdio.h> #define MAX 2 int stack[MAX]; int top = -1; int isFull() { return top == MAX - 1; } void push(int val) { if (!isFull()) { stack[++top] = val; } } int main() { push(5); push(15); if (isFull()) { printf("Stack is full\n"); } else { printf("Stack is not full\n"); } return 0; }
Attempts:
2 left
💡 Hint
Check how many elements the stack can hold and how many are pushed.
✗ Incorrect
The stack size is 2. After pushing two elements, top becomes 1 which equals MAX-1, so isFull() returns true and prints 'Stack is full'.
❓ Predict Output
advanced2:00remaining
Output after Mixed Stack Operations
What is the output of this C code after performing push and pop operations and checking if the stack is empty or full?
DSA C
#include <stdio.h> #define MAX 4 int stack[MAX]; int top = -1; int isEmpty() { return top == -1; } int isFull() { return top == MAX - 1; } void push(int val) { if (!isFull()) { stack[++top] = val; } } void pop() { if (!isEmpty()) { top--; } } int main() { push(1); push(2); push(3); pop(); push(4); push(5); if (isFull()) { printf("Stack is full\n"); } else if (isEmpty()) { printf("Stack is empty\n"); } else { printf("Stack is neither full nor empty\n"); } return 0; }
Attempts:
2 left
💡 Hint
Count the number of elements after all operations and compare with MAX.
✗ Incorrect
After pushing 1,2,3, popping once removes 3, then pushing 4 and 5 fills the stack to 4 elements. So isFull() returns true and prints 'Stack is full'.
❓ Predict Output
advanced2:00remaining
Stack State After Invalid Push
What is the output of this C code after trying to push an element when the stack is already full, then checking if the stack is full?
DSA C
#include <stdio.h> #define MAX 2 int stack[MAX]; int top = -1; int isFull() { return top == MAX - 1; } void push(int val) { if (!isFull()) { stack[++top] = val; } } int main() { push(100); push(200); push(300); // This push should be ignored if (isFull()) { printf("Stack is full\n"); } else { printf("Stack is not full\n"); } return 0; }
Attempts:
2 left
💡 Hint
The third push should not add an element because the stack is full.
✗ Incorrect
The stack size is 2. After pushing 100 and 200, the stack is full. The third push is ignored. So isFull() returns true and prints 'Stack is full'.
❓ Predict Output
expert2:00remaining
Output of Stack Empty and Full Checks with Underflow and Overflow Attempts
What is the output of this C code that attempts to pop from an empty stack and push beyond full capacity, then checks if the stack is empty or full?
DSA C
#include <stdio.h> #define MAX 3 int stack[MAX]; int top = -1; int isEmpty() { return top == -1; } int isFull() { return top == MAX - 1; } void push(int val) { if (!isFull()) { stack[++top] = val; } } void pop() { if (!isEmpty()) { top--; } } int main() { pop(); // pop on empty stack, should do nothing push(10); push(20); push(30); push(40); // push beyond full, ignored pop(); pop(); if (isEmpty()) { printf("Stack is empty\n"); } else if (isFull()) { printf("Stack is full\n"); } else { printf("Stack is neither full nor empty\n"); } return 0; }
Attempts:
2 left
💡 Hint
Track the stack size after each operation carefully.
✗ Incorrect
Pop on empty stack does nothing. Then push 3 elements fills stack. The 4th push is ignored. Two pops remove two elements, leaving one element. So stack is neither full nor empty.
