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 stack push and pop sequence?
Consider a stack implemented using an array with a maximum size of 3. The following operations are performed in order:
push(10), push(20), pop(), push(30), push(40), pop()
What is the printed state of the stack after these operations?
push(10), push(20), pop(), push(30), push(40), pop()
What is the printed state of the stack after these operations?
DSA Java
class Stack { int[] arr = new int[3]; int top = -1; void push(int x) { if (top == arr.length - 1) return; arr[++top] = x; } int pop() { if (top == -1) return -1; return arr[top--]; } void printStack() { for (int i = 0; i <= top; i++) { System.out.print(arr[i] + " -> "); } System.out.println("null"); } } public class Main { public static void main(String[] args) { Stack s = new Stack(); s.push(10); s.push(20); s.pop(); s.push(30); s.push(40); s.pop(); s.printStack(); } }
Attempts:
2 left
💡 Hint
Remember that pop removes the top element and push adds on top if there is space.
✗ Incorrect
Initially, push 10 and 20: stack is 10 -> 20 -> null. Pop removes 20: stack is 10 -> null. Push 30: stack is 10 -> 30 -> null. Push 40: 40 is added: stack is 10 -> 30 -> 40 -> null. Then pop removes 40: stack is 10 -> 30 -> null.
🧠 Conceptual
intermediateWhat happens when you try to push an element into a full stack implemented using an array?
If a stack is implemented using an array of fixed size 5 and currently holds 5 elements, what happens when you try to push another element?
Attempts:
2 left
💡 Hint
Think about fixed size arrays and their capacity limits.
✗ Incorrect
In a fixed size array stack, once the array is full, no more elements can be added. The push operation is ignored or rejected to prevent overflow.
🔧 Debug
advancedIdentify the error in this stack pop method implementation
What error will occur when calling pop() on an empty stack with this code?
DSA Java
class Stack { int[] arr = new int[3]; int top = -1; int pop() { return arr[top--]; } }
Attempts:
2 left
💡 Hint
What happens if top is -1 and you try to access arr[top]?
✗ Incorrect
If top is -1 (empty stack), accessing arr[top] tries to access arr[-1], which is invalid and causes ArrayIndexOutOfBoundsException.
❓ Predict Output
advancedWhat is the output after pushing and popping with boundary checks?
Given this stack implementation with boundary checks, what is the printed stack after these operations?
push(5), push(15), push(25), push(35), pop(), pop(), push(45), printStack()
push(5), push(15), push(25), push(35), pop(), pop(), push(45), printStack()
DSA Java
class Stack { int[] arr = new int[3]; int top = -1; void push(int x) { if (top == arr.length - 1) return; arr[++top] = x; } int pop() { if (top == -1) return -1; return arr[top--]; } void printStack() { for (int i = 0; i <= top; i++) { System.out.print(arr[i] + " -> "); } System.out.println("null"); } } public class Main { public static void main(String[] args) { Stack s = new Stack(); s.push(5); s.push(15); s.push(25); s.push(35); s.pop(); s.pop(); s.push(45); s.printStack(); } }
Attempts:
2 left
💡 Hint
Remember the stack size limit and that push ignores if full.
✗ Incorrect
Push 5,15,25 fills stack: 5->15->25->null. Push 35 ignored (full). Pop removes 25 and then 15: stack is 5->null. Push 45 adds on top: 5->45->null.
🧠 Conceptual
expertWhy is stack implemented using array faster than linked list in some cases?
Choose the best reason why a stack implemented using an array can be faster than one implemented using a linked list.
Attempts:
2 left
💡 Hint
Think about how memory is accessed in arrays vs linked lists.
✗ Incorrect
Arrays store elements in contiguous memory locations, which helps CPU cache to access data faster compared to linked lists where nodes are scattered in memory.
