Try it first — give it 25 minutes
0/4 revealedStuck? Take one hint at a time. The struggle is where the pattern sticks.
The problem in plain English
Design a class MinStack with four operations:
push(val): putvalon top;pop(): remove the top element;top(): return the top element without removing it;getMin(): return the smallest element currently in the stack.
Every operation must run in O(1) time. pop, top and getMin are only called on a non-empty stack. Values can be anywhere in the 32-bit integer range, and there are up to 3 × 10⁴ calls.
Example: push(-2), push(0), push(-3), getMin() → -3, pop(), top() → 0, getMin() → -2.
Start with brute force
Use a normal stack and, for getMin, scan every element and return the smallest.
It's correct, but getMin is O(n). With 3 × 10⁴ calls on a stack that holds up to 3 × 10⁴ items, that's up to about 9 × 10⁸ steps, and it breaks the O(1) rule anyway.
Building the intuition
The first thing everyone tries is one extra variable currentMin. Push is easy: currentMin = min(currentMin, val).
Now pop. Say the stack is [5, 3, 7] and currentMin = 3. You pop 7: fine, the minimum is still 3. You pop 3: what's the new minimum? You'd have to scan [5] to find out. One variable has forgotten history you now need.
So what history do you need? Notice something special about stacks: only the top ever changes. While 3 sits on the stack, everything below it stays exactly as it was. That means the answer to "what's the minimum of me and everything below me?" is fixed the moment 3 is pushed, and stays true until 3 is popped.
So just write that answer down at push time, right next to the value:
| stack (bottom → top) | value | min at or below |
|---|---|---|
| 1st | 5 | 5 |
| 2nd | 3 | 3 |
| 3rd | 7 | 3 |
getMin reads the top row's second column. When you pop a row, the row below still has its own correct minimum written down. Nothing needs recomputing.
The optimal approach
- Keep one stack of pairs
(value, minSoFar). push(val): if the stack is empty,minSoFar = val; otherwiseminSoFar = min(val, top.minSoFar). Push the pair.pop(): remove the top pair. That's it.top(): return the top pair'svalue.getMin(): return the top pair'sminSoFar.
The subtle detail is duplicates of the minimum. A popular variation keeps two stacks and only pushes to the min-stack when val < currentMin. Push 0, 1, 0, pop once, and getMin must still say 0, but that version has already thrown its only 0 away. Use <= there, or use pairs as we do here, and the problem goes away.
The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
class MinStack:
def __init__(self):
# each entry is (value, smallest value at or below this entry)
self.stack = []
def push(self, val: int) -> None:
current_min = min(val, self.stack[-1][1]) if self.stack else val
self.stack.append((val, current_min))
def pop(self) -> None:
# the entry below still remembers the minimum from before this push
self.stack.pop()
def top(self) -> int:
return self.stack[-1][0]
def getMin(self) -> int:
return self.stack[-1][1]class MinStack {
// each entry is {value, smallest value at or below this entry}
vector<pair<int, int>> stack;
public:
MinStack() {}
void push(int val) {
int currentMin = stack.empty() ? val : min(val, stack.back().second);
stack.push_back({val, currentMin});
}
void pop() {
// the entry below still remembers the minimum from before this push
stack.pop_back();
}
int top() {
return stack.back().first;
}
int getMin() {
return stack.back().second;
}
};class MinStack {
// each entry is {value, smallest value at or below this entry}
private final Deque<int[]> stack = new ArrayDeque<>();
public MinStack() {}
public void push(int val) {
int currentMin = stack.isEmpty() ? val : Math.min(val, stack.peek()[1]);
stack.push(new int[] { val, currentMin });
}
public void pop() {
// the entry below still remembers the minimum from before this push
stack.pop();
}
public int top() {
return stack.peek()[0];
}
public int getMin() {
return stack.peek()[1];
}
}class MinStack {
constructor() {
// each entry is [value, smallest value at or below this entry]
this.stack = [];
}
/**
* @param {number} val
* @return {void}
*/
push(val) {
const n = this.stack.length;
const currentMin = n === 0 ? val : Math.min(val, this.stack[n - 1][1]);
this.stack.push([val, currentMin]);
}
/**
* @return {void}
*/
pop() {
// the entry below still remembers the minimum from before this push
this.stack.pop();
}
/**
* @return {number}
*/
top() {
return this.stack[this.stack.length - 1][0];
}
/**
* @return {number}
*/
getMin() {
return this.stack[this.stack.length - 1][1];
}
}Dry run
push(-2), push(0), push(-3), getMin(), pop(), top(), getMin()
| call | stack after (value, min) | returns |
|---|---|---|
push(-2) | (-2,-2) | |
push(0) | (-2,-2) (0,-2) | |
push(-3) | (-2,-2) (0,-2) (-3,-3) | |
getMin() | unchanged | -3 |
pop() | (-2,-2) (0,-2) | |
top() | unchanged | 0 |
getMin() | unchanged | -2 |
After popping -3, the new top (0, -2) already knew that the minimum below it was -2.
Complexity
- Time: O(1) per operation. Each call touches only the top of the stack.
- Space: O(n). We store two numbers per element instead of one. Still O(n).
Edge cases and traps
- Repeated minimums.
push(0), push(1), push(0), pop(): the minimum is still0. The two-stack version with a strict<fails this. - Extreme values.
INT_MINandINT_MAXmust work. Don't startcurrentMinat a sentinel likeINT_MAXand then rely on it to mean "empty". Check emptiness explicitly. - Java
Integercomparison. With twoDeque<Integer>stacks,a.peek() == b.peek()compares references and breaks for values outside −128..127. Use.equalsor unbox. The pair version avoids comparing boxed values at all. - Mixing up
topandgetMin.topis the last pushed value, not the smallest.
Say it in the interview
"A single min variable breaks when you pop the minimum, because you've lost what the minimum was before it. But a stack only changes at the top, so for each element the minimum of itself and everything below is fixed while it's on the stack. I'll store that next to each value as a pair when I push. Then getMin is just the top pair's second field, and pop needs no work, because the pair below already knows its own minimum. Every operation is O(1), and space is O(n)."
Follow-ups interviewers ask
- "Can you use less memory?" Use a second stack that only grows when
val <= currentMin, and pop it only when the popped value equals its top. Same worst case, much less memory in typical runs. - "Can you do it with O(1) extra space?" Yes: store
val − minas a difference, using 64-bit values to avoid overflow, and decode the previous minimum on pop. It's clever but fragile. Mention it, and write the pair version. - "Now build a Max Stack that can also pop the maximum." That needs more than a stack: usually a doubly linked list plus a sorted map, at O(log n) per operation.
Practise the same idea
This is Day 30 of a free 90-day plan 🐸
90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.
Get the full roadmap — free