Sitemap
Alan Martins

Alan Martins

Senior Software Developer at Nubank

Min Stack

3 min readJun 23, 2025

--

How to get the minimum value from a stack?

This is an interesting problem that looks simple at first. The basic stack operations are easy, but the challenge is how to get the minimum value quickly, in constant time.

Understanding the Problem

We are required to design a stack that supports push, pop, top, and the most challenging operation — getMin.

The operations push, which adds a value to the stack; pop, which removes the last value; and top, which retrieves the last value added (i.e., the top of the stack), can all be implemented with constant time complexity — O(1).

Get Alan Martins’s stories in your inbox

Join Medium for free to get updates from this writer.

Here’s how they work using a simple array-based stack in Python:

stack.append(val)  # Adds a value to the end of the array (top of the stack)
stack.pop() # Removes the last value added (top of the stack)
stack[-1] # Retrieves the last value without removing it

But what about getMin?
How can we retrieve the minimum value in the stack while maintaining constant time complexity — O(1)?

How the Algorithm Works

The problem can be broken into simple steps:

  1. Create an empty array stack to store the values.
  2. For the operations push, pop, and top, the behavior is straightforward:
    push: add a value to the end of the stack.
    pop: remove the last value from the stack.
    top: return the last value in the stack.
  3. The real challenge is getMin. There are two ways to approach it:
    - A simple (but inefficient) way is to use Python’s built-in min() function to scan the entire stack and return the smallest value. This works but has linear time complexity — O(n).
    - A more efficient way is to store the minimum value at each step. For every push, along with storing the value, we also store the minimum value so far. This way, getMin can retrieve the current minimum instantly in constant time — O(1).
  4. With this idea, every time we pop, we remove both the value and its corresponding minimum at that point in time.
  5. Both top and getMin simply return the last stored values — one for the actual stack and one for the minimum tracker.

Brute-force solution:

Step 1: Create the stack

  • Create an empty array stack to store the values.
stack = []

Step 2: Implement basic operations

  • push: Add a value to the end of the stack.
stack.append(val)
  • pop: Remove the last value from the stack.
stack.pop()
  • top: Return the last value without removing it.
stack[-1]

Step 3: Get the minimum value

  • Use Python’s built-in min() function to scan the entire stack and return the smallest value.
min(stack)

This solution works, but getMin() has O(n) time complexity since it needs to scan the whole stack. The optimal approach improves that.

Full solution

class MinStack:
def __init__(self):
self.stack = []

def push(self, val: int) -> None:
self.stack.append(val)

def pop(self) -> None:
self.stack.pop()

def top(self) -> int:
return self.stack[-1]

def getMin(self) -> int:
return min(self.stack)

Optimal solution

Step 1: Initialize the stack and the min tracker

  • Create two stacks:
  • stack for storing the values.
  • min_stack for tracking the minimum value at each step.
stack = []
min_stack = []

Step 2: Implement the push operation

  • Push the value to stack.
  • For min_stack:
  • If it’s empty or the new value is less than or equal to the current minimum, push the new value.
  • Otherwise, repeat the current minimum at the top of min_stack.
stack.append(val)
if not min_stack or val <= min_stack[-1]:
min_stack.append(val)
else:
min_stack.append(min_stack[-1])

Step 3: Implement the pop operation

  • Remove the last value from both stack and min_stack.
stack.pop()
min_stack.pop()

Step 4: Implement top and getMin

  • top: Return the last value from stack.
stack[-1]
  • getMin: Return the last value from min_stack — this is the minimum value at the current state.
min_stack[-1]

This solution has O(1) time complexity for all operations, including getMin(), and uses extra space to maintain the minimums.

Full solution

class MinStack:
def __init__(self):
self.stack = []
self.min_stack = []

def push(self, val: int) -> None:
self.stack.append(val)
if not self.min_stack or val <= self.min_stack[-1]:
self.min_stack.append(val)
else:
self.min_stack.append(self.min_stack[-1])

def pop(self) -> None:
self.stack.pop()
self.min_stack.pop()

def top(self) -> int:
return self.stack[-1]

def getMin(self) -> int:
return self.min_stack[-1]

References

--

--

Written by Alan Martins

Senior Software Developer at Nubank