Challenge - 5 Problems
Min Stack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Min Stack after push and pop operations
What is the output of the min stack after performing the following operations?
Operations:
push(3), push(5), push(2), push(1), pop(), getMin(), pop(), getMin()
Operations:
push(3), push(5), push(2), push(1), pop(), getMin(), pop(), getMin()
DSA Java
class MinStack { private Stack<Integer> stack = new Stack<>(); private Stack<Integer> minStack = new Stack<>(); public void push(int x) { stack.push(x); if (minStack.isEmpty() || x <= minStack.peek()) { minStack.push(x); } } public void pop() { if (stack.peek().equals(minStack.peek())) { minStack.pop(); } stack.pop(); } public int getMin() { return minStack.peek(); } } MinStack ms = new MinStack(); ms.push(3); ms.push(5); ms.push(2); ms.push(1); ms.pop(); int firstMin = ms.getMin(); ms.pop(); int secondMin = ms.getMin(); System.out.println(firstMin + "," + secondMin);
Attempts:
2 left
💡 Hint
Remember that getMin returns the smallest element currently in the stack.
✗ Incorrect
After pushing 3,5,2,1, the stack is [3,5,2,1]. Popping once removes 1. getMin now returns 2. Popping again removes 2. getMin now returns 3.
🧠 Conceptual
intermediateUnderstanding Min Stack Space Complexity
What is the worst-case space complexity of a Min Stack that stores the minimum values alongside the main stack?
Attempts:
2 left
💡 Hint
Consider when every pushed element is smaller than the previous minimum.
✗ Incorrect
In the worst case, every new element is smaller or equal to the current minimum, so the min stack stores all elements, leading to O(n) space.
🔧 Debug
advancedIdentify the bug in Min Stack pop method
What error will occur when running this pop method in Min Stack if the stack is empty?
DSA Java
public void pop() {
if (stack.peek().equals(minStack.peek())) {
minStack.pop();
}
stack.pop();
}Attempts:
2 left
💡 Hint
What happens if pop is called on an empty stack?
✗ Incorrect
Calling peek() on an empty stack throws EmptyStackException in Java.
❓ Predict Output
advancedOutput of Min Stack after mixed operations
What is printed after executing these operations on the Min Stack?
push(10), push(20), push(5), getMin(), pop(), getMin(), pop(), getMin()
push(10), push(20), push(5), getMin(), pop(), getMin(), pop(), getMin()
DSA Java
MinStack ms = new MinStack(); ms.push(10); ms.push(20); ms.push(5); System.out.print(ms.getMin() + ","); ms.pop(); System.out.print(ms.getMin() + ","); ms.pop(); System.out.print(ms.getMin());
Attempts:
2 left
💡 Hint
Track the minimum after each pop carefully.
✗ Incorrect
After pushing 10,20,5 min is 5. Pop removes 5, min becomes 10. Pop removes 20, min remains 10.
🧠 Conceptual
expertOptimal Min Stack Design for Space Efficiency
Which approach optimizes space usage in a Min Stack by storing minimum values only when they change?
Attempts:
2 left
💡 Hint
Think about pushing minimum values only when they update.
✗ Incorrect
Pushing minimum only when it changes reduces space by avoiding duplicates in min stack.
