20. Min Stack
easyAsked at PlaidDesign a stack that supports push, pop, top, and retrieving the minimum element in constant time. Plaid asks this because tracking the running minimum balance over a transaction stream needs exactly this primitive.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Plaid loops.
- Glassdoor (2025)— Plaid backend onsite — running-min over balance series.
- Blind (2026)— Plaid SWE II screen.
Problem
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the MinStack class with methods push(val), pop(), top(), and getMin().
Constraints
-2^31 <= val <= 2^31 - 1Methods pop, top and getMin operations will always be called on non-empty stacks.At most 3 * 10^4 calls will be made.
Examples
Example 1
push(-2); push(0); push(-3); getMin(); pop(); top(); getMin()-3, then 0, then -2Approaches
1. Scan for min on each call
Single array; scan all elements when getMin is called.
- Time
- O(n) per getMin, O(1) others
- Space
- O(n)
class MinStack {
constructor() { this.data = []; }
push(v) { this.data.push(v); }
pop() { this.data.pop(); }
top() { return this.data.at(-1); }
getMin() { return Math.min(...this.data); }
}Tradeoff: Fails the O(1) requirement. Math.min(...arr) also crashes when the stack is huge (spread limit).
2. Auxiliary min-stack
Maintain a parallel stack where each entry is the min of the prefix ending at that index.
- Time
- O(1) all operations
- Space
- O(n)
class MinStack {
constructor() { this.data = []; this.mins = []; }
push(v) {
this.data.push(v);
this.mins.push(this.mins.length === 0 ? v : Math.min(this.mins.at(-1), v));
}
pop() { this.data.pop(); this.mins.pop(); }
top() { return this.data.at(-1); }
getMin() { return this.mins.at(-1); }
}Tradeoff: Constant time everywhere. The mins stack stores 'min up to and including this position' — the invariant makes pop work cleanly without recomputation.
Plaid-specific tips
Plaid grades this on the cleanliness of the auxiliary-stack invariant. Articulate 'mins[i] is the min of data[0..i]' explicitly. Bonus signal: discuss the memory-optimization variant where you only push to mins when v <= current min — useful when most pushes are increasing.
Common mistakes
- Not pushing to mins on every push — breaks pop's bookkeeping.
- Using Math.min(...this.data) — exceeds spread limit and is O(n).
- Pushing to mins only when smaller — works but pop has to check before popping mins.
Follow-up questions
An interviewer at Plaid may pivot to one of these next:
- Save memory by only pushing to mins when the new value is <= current min (LC 155 optimal).
- Max stack — same trick, mirrored.
- Sliding window min on a stream (LC 239).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why mirror the data stack?
Each entry stores the min of the prefix ending there. When you pop, the new top of mins is automatically the min of the remaining prefix.
What's the memory-optimal variant?
Only push to mins when v <= mins.top(). Then pop from mins only when data.top() === mins.top(). Saves space when pushes are mostly increasing — common for balance histories that trend up.
Practice these live with InterviewChamp.AI
Drill Min Stack and other Plaid interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →