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 Java stack check?
Consider a stack of size 3. After pushing two elements, what does the
isFull() method return?DSA Java
class Stack { int[] arr = new int[3]; int top = -1; boolean isFull() { return top == arr.length - 1; } void push(int x) { if (!isFull()) { arr[++top] = x; } } public static void main(String[] args) { Stack s = new Stack(); s.push(10); s.push(20); System.out.println(s.isFull()); } }
Attempts:
2 left
💡 Hint
Think about how many elements the stack can hold and how many are pushed.
✗ Incorrect
The stack size is 3, but only 2 elements are pushed. So, top is 1, and isFull() checks if top == 2, which is false.
❓ Predict Output
intermediateWhat does this code print about stack emptiness?
After pushing and popping elements, what does
isEmpty() return?DSA Java
class Stack { int[] arr = new int[2]; int top = -1; boolean isEmpty() { return top == -1; } void push(int x) { if (top < arr.length - 1) { arr[++top] = x; } } void pop() { if (!isEmpty()) { top--; } } public static void main(String[] args) { Stack s = new Stack(); s.push(5); s.push(10); s.pop(); s.pop(); System.out.println(s.isEmpty()); } }
Attempts:
2 left
💡 Hint
Check how many elements remain after popping twice.
✗ Incorrect
After pushing two elements and popping twice, the stack is empty, so isEmpty() returns true.
❓ Predict Output
advancedWhat error occurs when pushing on a full stack?
What happens when you push an element onto a full stack without checking if it is full?
DSA Java
class Stack { int[] arr = new int[2]; int top = -1; void push(int x) { arr[++top] = x; } public static void main(String[] args) { Stack s = new Stack(); s.push(1); s.push(2); s.push(3); } }
Attempts:
2 left
💡 Hint
Consider what happens when the array index goes beyond its size.
✗ Incorrect
The array size is 2, but push is called 3 times without checking. The third push tries to access index 2, causing ArrayIndexOutOfBoundsException.
🧠 Conceptual
advancedWhich condition correctly checks if a stack is empty?
Given a stack with a top pointer, which condition correctly returns true if the stack is empty?
Attempts:
2 left
💡 Hint
Think about the initial value of top before any push.
✗ Incorrect
The top is initialized to -1 when the stack is empty. So, top == -1 means empty stack.
❓ Predict Output
expertWhat is the printed output after these stack operations?
Analyze the stack operations and determine the printed output.
DSA Java
class Stack { int[] arr = new int[3]; int top = -1; boolean isFull() { return top == arr.length - 1; } boolean isEmpty() { return top == -1; } void push(int x) { if (!isFull()) { arr[++top] = x; } } int pop() { if (!isEmpty()) { return arr[top--]; } return -1; } public static void main(String[] args) { Stack s = new Stack(); s.push(7); s.push(14); s.push(21); System.out.print(s.pop() + " "); s.push(28); System.out.print(s.pop() + " "); System.out.print(s.pop() + " "); System.out.print(s.isEmpty()); } }
Attempts:
2 left
💡 Hint
Trace each push and pop step carefully.
✗ Incorrect
Push 7,14,21 fills stack. Pop returns 21. Push 28. Pop returns 28. Pop returns 14. Stack still has 7, so isEmpty() is false.
