ML Interview Prep
📚 PracticeMediumAlgorithm ProblemCoding Ready

Min Stack

stackdesign
LeetCode #155
Updated Dec 20, 2025

Question

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

LeetCode: Min Stack

Implement the MinStack class:

  • MinStack() initializes the stack object
  • void push(int val) pushes the element val onto the stack
  • void pop() removes the element on the top of the stack
  • int top() gets the top element of the stack
  • int getMin() retrieves the minimum element in the stack

All operations must run in O(1) time.

Example:

Input:
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]

Output:
[null,null,null,null,-3,null,0,-2]

Explanation:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); // return -3
minStack.pop();
minStack.top();    // return 0
minStack.getMin(); // return -2

Hints

Hint 1

You need to track the minimum at each level of the stack. Think about using an additional data structure.

Hint 2

Use two stacks: one for regular values, one to track minimums. The min stack always has the current minimum at its top.

Hint 3

When pushing, only add to min_stack if the new value is less than or equal to the current minimum. When popping, also pop from min_stack if it matches.


Your Solution

python
Auto-saves every 30s

Try solving the problem first before viewing the solution


Learning Resources

Related Problems

0:00time spent