Algorithms · Design a tiny system

Min Stack

A stack that tells you its smallest item instantly.

The problem

Build a stack with push, pop, top and get_min, where every operation is fast, including get_min. The tests send a list of instructions to play.

[["push", -2], ["push", 0], ["push", -3], ["get_min"], ["pop"], ["top"], ["get_min"]][-3, 0, -2]
[["push", 5], ["get_min"], ["push", 1], ["get_min"], ["pop"], ["get_min"]][5, 1, 5]
Design a tiny system. Pick the right container. The books on your desk vs. the library: keep what you use close at hand.

Start with the way that works

This version is correct. It is also the slow one. Read it first: the fast way is easier to find once you can see what the slow way wastes.

class MinStack:a stack that also knows its smallest item
def __init__(self):when it's created…
self.items = []…it starts empty
def push(self, x):add x on top
self.items.append(x)put it on the pile
def pop(self):remove the top item
self.items.pop()take it off the pile
def top(self):look at the top item
return self.items[-1]the last one added
def get_min(self):the smallest item
return min(self.items)look through the whole pile (slow!)

Now ask: what if there were a billion?

Paying a little extra memory to answer instantly is the most common trade in real systems. Trading engines keep “best price so far” right next to each order for the same reason.

Aim for O(n)🧩 classic

Think it through

When you push a number, do you already know the smallest in the stack?

  • Yes: the smallest so far and the new number
  • No, you have to look
Show the answer

Yes: the smallest so far and the new number. You knew the minimum before; the new one is the smaller of that and the new number. Write it down now and you never need to look again.

After a pop, how do you know the new minimum?

  • Look through the stack
  • Each item remembered the minimum from when it was pushed
Show the answer

Each item remembered the minimum from when it was pushed. Keep the minimums in a second stack that pushes and pops in step. Spending a little memory to save time is a trade you’ll make again and again.

Stuck? 3 nudges, smallest first
  1. min(self.items) looks through the whole pile every time someone asks.
  2. When you push, you already know the smallest item so far. Write it down next to the new item.
  3. Keep a second list mins. On push, append min(x, mins[-1]); on pop, pop both; get_min returns mins[-1].

The fast way

Try it yourself before you open this. The slow version above already passes every test, so you can change it one step at a time.

Show the fast version
class MinStack:a stack that also knows its smallest item
def __init__(self):when it's created…
self.items = []…it starts empty
self.mins = []mins[i] = smallest of items[0..i]
def push(self, x):add x on top
self.items.append(x)put it on the pile
self.mins.append(min(x, self.mins[-1]) if self.mins else x)remember the smallest so far
def pop(self):remove the top item
self.items.pop()take it off the pile
self.mins.pop()and forget its smallest-so-far
def top(self):look at the top item
return self.items[-1]the last one added
def get_min(self):the smallest item
return self.mins[-1]already written down: one step

Solve it yourself.

It runs right in the page: the tests, the gremlins that try to break it, and a race up to a billion. Free, nothing to install.

More with design a tiny system