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 operation sequence?
Consider a stack where we push integers 1, 2, 3 in order, then pop one element, then push 4. What is the printed stack from bottom to top?
DSA Java
Stack<Integer> stack = new Stack<>(); stack.push(1); stack.push(2); stack.push(3); stack.pop(); stack.push(4); System.out.print(stack);
Attempts:
2 left
💡 Hint
Remember stack pops the last pushed item first.
✗ Incorrect
We push 1, 2, 3. Stack is [1, 2, 3]. Pop removes 3. Stack is [1, 2]. Push 4 adds on top. Stack is [1, 2, 4].
🧠 Conceptual
intermediateWhich operation follows the LIFO principle in a stack?
Choose the operation that always removes the most recently added element from a stack.
Attempts:
2 left
💡 Hint
LIFO means last in, first out.
✗ Incorrect
Pop removes the last element added, following LIFO. Push adds, peek views top, isEmpty checks emptiness.
🔧 Debug
advancedWhat error occurs when popping from an empty stack?
Given this code snippet, what error will it throw when pop() is called on an empty stack?
DSA Java
Stack<Integer> stack = new Stack<>(); stack.pop();
Attempts:
2 left
💡 Hint
Check Java stack exception types for empty pop.
✗ Incorrect
Java's Stack class throws EmptyStackException when pop is called on empty stack.
🚀 Application
advancedWhat is the stack content after this sequence of operations?
Start with an empty stack. Perform: push(5), push(10), pop(), push(15), pop(), push(20). What is the stack from bottom to top?
DSA Java
Stack<Integer> stack = new Stack<>(); stack.push(5); stack.push(10); stack.pop(); stack.push(15); stack.pop(); stack.push(20); System.out.print(stack);
Attempts:
2 left
💡 Hint
Pop removes the last pushed element each time.
✗ Incorrect
Push 5: [5]
Push 10: [5,10]
Pop removes 10: [5]
Push 15: [5,15]
Pop removes 15: [5]
Push 20: [5,20]
❓ Predict Output
expertWhat is the output of this Java stack code with multiple pops?
Given the code below, what is printed after all operations?
DSA Java
Stack<String> stack = new Stack<>(); stack.push("a"); stack.push("b"); stack.push("c"); stack.pop(); stack.pop(); stack.push("d"); System.out.print(stack);
Attempts:
2 left
💡 Hint
Pop removes last pushed elements in order.
✗ Incorrect
Push a: [a]
Push b: [a,b]
Push c: [a,b,c]
Pop removes c: [a,b]
Pop removes b: [a]
Push d: [a,d]
