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 after pushing elements on the stack?
Consider a stack initially empty. We push the elements 5, 10, and 15 in this order. What is the printed state of the stack after these operations?
DSA Java
class Stack { private int[] arr = new int[10]; private int top = -1; public void push(int x) { if (top < arr.length - 1) { arr[++top] = x; } } public void printStack() { for (int i = top; i >= 0; i--) { System.out.print(arr[i] + " -> "); } System.out.println("null"); } } public class Main { public static void main(String[] args) { Stack stack = new Stack(); stack.push(5); stack.push(10); stack.push(15); stack.printStack(); } }
Attempts:
2 left
💡 Hint
Remember, stack is Last In First Out (LIFO). The last pushed element is on top.
✗ Incorrect
The stack pushes 5 first, then 10, then 15. The top is 15, so printing from top to bottom shows 15 -> 10 -> 5 -> null.
❓ Predict Output
intermediateWhat happens if we push beyond stack capacity?
Given a stack with capacity 3, what is the output after pushing 1, 2, 3, and then 4?
DSA Java
class Stack { private int[] arr = new int[3]; private int top = -1; public void push(int x) { if (top < arr.length - 1) { arr[++top] = x; } } public void printStack() { for (int i = top; i >= 0; i--) { System.out.print(arr[i] + " -> "); } System.out.println("null"); } } public class Main { public static void main(String[] args) { Stack stack = new Stack(); stack.push(1); stack.push(2); stack.push(3); stack.push(4); stack.printStack(); } }
Attempts:
2 left
💡 Hint
Check if the push method prevents adding elements beyond capacity.
✗ Incorrect
The stack capacity is 3. The fourth push is ignored because the stack is full. So the stack contains 3, 2, 1 from top to bottom.
🔧 Debug
advancedIdentify the bug in the push method
What is the problem with this push method in the stack implementation?
DSA Java
public void push(int x) { if (top <= arr.length) { arr[++top] = x; } }
Attempts:
2 left
💡 Hint
Check the condition that controls pushing elements.
✗ Incorrect
The condition 'top <= arr.length' allows top to reach arr.length, which is out of array bounds (max index is arr.length - 1). This causes an error when pushing.
🧠 Conceptual
advancedWhat is the time complexity of push operation on a stack?
Assuming a stack implemented with an array, what is the time complexity of the push operation?
Attempts:
2 left
💡 Hint
Push adds one element at the top without looping over elements.
✗ Incorrect
Push operation adds an element at the top index directly, so it takes constant time O(1).
❓ Predict Output
expertWhat is the stack state after mixed push and pop operations?
Given the following operations on an empty stack: push(7), push(14), pop(), push(21), push(28), pop(), push(35). What is the printed stack state?
DSA Java
class Stack { private int[] arr = new int[10]; private int top = -1; public void push(int x) { if (top < arr.length - 1) { arr[++top] = x; } } public void pop() { if (top >= 0) { top--; } } public void printStack() { for (int i = top; i >= 0; i--) { System.out.print(arr[i] + " -> "); } System.out.println("null"); } } public class Main { public static void main(String[] args) { Stack stack = new Stack(); stack.push(7); stack.push(14); stack.pop(); stack.push(21); stack.push(28); stack.pop(); stack.push(35); stack.printStack(); } }
Attempts:
2 left
💡 Hint
Track each push and pop step carefully to see which elements remain.
✗ Incorrect
Operations:
- push 7: stack = [7]
- push 14: stack = [7,14]
- pop: removes 14, stack = [7]
- push 21: stack = [7,21]
- push 28: stack = [7,21,28]
- pop: removes 28, stack = [7,21]
- push 35: stack = [7,21,35]
Printed top to bottom: 35 -> 21 -> 7 -> null
