Min Stack
On this page
Problem statement
Design a stack that supports push(x), pop(), top() and getMin(), all in O(1) time.
getMinreturns the smallest value currently in the stack. You may assumepop,topandgetMinare only called on a non-empty stack.
A short design question about remembering history: the minimum changes when you pop, and you can't afford to search for it again. It is often asked as its mirror, a Max Stack, where the hard part is an extra popMax(); that version is covered below. Original problem: LeetCode 155 (Max Stack: LeetCode 716).
Example

push(5); push(2); push(7)
getMin() -> 2
pop() -> 7
pop() -> 2
getMin() -> 5
Constraints (assumed)
- Up to 3 × 10⁴ calls; values fit in a 32-bit integer.
What this question tests
- Noticing that a single minimum variable can't be restored after a pop.
- Augmenting a data structure: storing extra information alongside each element.
- For the Max Stack version: combining a stack with a heap or ordered map and keeping them consistent.
Questions to ask first
- Can the stack be empty when getMin is called? (Here: no. Otherwise decide whether to throw or return a sentinel.)
- Can values repeat? (Yes, and duplicates of the minimum are the classic trap.)
- Does pop need to return the value? (Here: yes, it is handy.)
- Is it a min stack, a max stack, or both? If there is a
popMax/popMin, the design changes.
Try it yourself first
You have the full problem. Sketch your answer, then sign up free to read the complete walkthrough.
- The complete design, flow by flow, with pictures
- Deep dives into the hard parts
- What mid, senior and staff candidates are expected to say
- Full company question lists
Free forever. No card needed.