Coding · must-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix
Min Stack
Problem
Design a stack that, in addition to the usual push, pop, and top, can report the smallest element currently in the stack. Every operation, including getMin, must run in constant time.
Implement the class MinStack:
MinStack()creates an empty stack.push(val)pushesvalon top.pop()removes the top element.top()returns the top element without removing it.getMin()returns the minimum element currently in the stack.
Constraints:
- Every value fits in a signed 32-bit integer.
pop,top, andgetMinare never called on an empty stack.- Up to 50,000 operations are issued in total.
Examples
push(4), push(9), push(1), getMin()→1. Thenpop(), top()→9, andgetMin()→4, because once1is gone the smallest remaining value is4.push(-1), push(-1), push(3), getMin(), pop(), pop(), getMin()→-1, -1. Two copies of the minimum were pushed, so popping one copy must not lose the other.push(12), getMin()→12. A single element is its own minimum.
Hints
- Scanning the stack on every
getMinis O(n). What could you record at push time so the answer is already known? - The minimum of the stack after a pop is exactly the minimum it had before the popped element was pushed. That history is itself stack-shaped.
- Keep a second stack (or a tuple per entry) holding "minimum so far" alongside every element.
Last reviewed 8 September 2026
Locked
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Secure payment by Stripe · instant access · refund questions: [email protected]
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.