Min Stack Design in DSA Java - Time & Space Complexity
We want to understand how fast the Min Stack operations run as the number of elements grows.
Specifically, how the time to push, pop, or get the minimum changes with more data.
Analyze the time complexity of the following Min Stack implementation.
import java.util.Stack;
class MinStack {
private Stack stack = new Stack<>();
private Stack 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.pop().equals(minStack.peek())) {
minStack.pop();
}
}
public int getMin() {
return minStack.peek();
}
}
This code keeps track of the minimum value in a stack efficiently using two stacks.
- Primary operation: push, pop, and peek on stacks.
- How many times: Each operation runs once per call, no loops inside.
Each push or pop does a fixed number of steps regardless of stack size.
| Input Size (n) | Approx. Operations per push/pop |
|---|---|
| 10 | 2-3 steps |
| 100 | 2-3 steps |
| 1000 | 2-3 steps |
Pattern observation: The number of steps stays about the same no matter how big the stack is.
Time Complexity: O(1)
This means each operation takes the same short time no matter how many items are in the stack.
[X] Wrong: "Finding the minimum must check all elements, so it takes longer as the stack grows."
[OK] Correct: The min stack keeps track of minimums as we go, so getting the minimum is always quick and does not scan the whole stack.
Knowing how to keep track of minimums efficiently shows you can design smart data structures that save time.
This skill is useful for many problems where quick access to special values is needed.
"What if we used only one stack and searched for the minimum each time? How would the time complexity change?"
Practice
Min Stack data structure?Solution
Step 1: Understand stack operations
A stack supports push, pop, and top operations in LIFO order.Step 2: Identify Min Stack feature
Min Stack extends stack by allowing retrieval of minimum element in O(1) time.Final Answer:
To retrieve the minimum element in constant time along with stack operations -> Option AQuick Check:
Min Stack = constant time min retrieval [OK]
- Thinking Min Stack sorts elements automatically
- Assuming Min Stack stores only minimum elements
- Confusing Min Stack with reversing stack order
val onto a Min Stack?Solution
Step 1: Recall Java method syntax
Java method signature starts with access modifier, return type, method name, and parameters.Step 2: Check each option
public void push(int val) follows correct syntax: public void push(int val). Others have wrong order or return type.Final Answer:
public void push(int val) -> Option CQuick Check:
Java method syntax = public void push(int val) [OK]
- Placing 'public' after method name
- Using wrong return type for void methods
- Incorrect parameter placement
MinStack minStack = new MinStack(); minStack.push(3); minStack.push(5); minStack.push(2); minStack.push(1); minStack.pop(); int min = minStack.getMin(); System.out.println(min);
What will be printed?
Solution
Step 1: Trace push operations and min values
Push 3 (min=3), push 5 (min=3), push 2 (min=2), push 1 (min=1).Step 2: Pop removes top (1), min updates to previous min (2), then getMin returns 2.
Final Answer:
2 -> Option AQuick Check:
After pop, min = 2 [OK]
- Assuming min stays 1 after pop
- Confusing top element with min
- Ignoring min update on pop
public void pop() {
if(stack.isEmpty()) return;
int top = stack.pop();
if(top == minStack.peek()) {
minStack.pop();
}
}What is the main issue here?
Solution
Step 1: Analyze pop method safety
Before calling minStack.peek(), we must ensure minStack is not empty to avoid error.Step 2: Identify missing empty check on minStack
Code checks stack but not minStack emptiness before peek, risking exception.Final Answer:
It does not handle empty minStack before peek -> Option DQuick Check:
Check minStack empty before peek [OK]
- Ignoring minStack empty check
- Popping stack twice
- Confusing peek and pop methods
push, pop, top, and getMin all in constant time. Which approach is best?Solution
Step 1: Understand time complexity requirements
All operations must be O(1), so scanning or traversing is not allowed.Step 2: Evaluate approaches
Two stacks approach keeps track of minimums efficiently in O(1) time.Final Answer:
Use two stacks: one for all values, one for current minimums -> Option BQuick Check:
Two stacks = O(1) min retrieval [OK]
- Using scanning which is O(n)
- Using queue which is not stack
- Traversing linked list on getMin
Ready to prove it?
Pass the test and this pattern moves unknown
