Coding interview problems โบ Stack & Monotonic Stack
๐ Stack & Monotonic Stack Medium
Min Stack
Min Stack explained: store the running minimum next to every value so push, pop, top and getMin are all O(1). Tested Python, pitfalls and a live stack visual.
Also searched as: stack with getMin in O(1) ยท design a min stack ยท special stack with minimum
Try it live Free
Stacks & the Monotonic Stack
Use the stack mode to watch pushes and pops happen only at the top; this design adds one extra field to every pushed entry, the minimum so far.
The problem
Design a stack that supports push(x), pop(), top() and get_min(), where get_min returns the smallest element currently in the stack. Every operation must take O(1) time. You may assume pop, top and get_min are only called on a non-empty stack.
Once 2 is popped, the minimum goes back to 5.
The key insight
The trouble is that popping the current minimum should reveal the previous minimum. So remember it: store each value together with the minimum of the stack at the moment it was pushed. The top entry then always carries the current minimum.
get_min works but makes that operation O(n).Step-by-step approach
- Keep one list of pairs
(value, min_so_far). - On
push(x), the new minimum isxif the stack is empty, elsemin(x, current_min); append(x, new_min). pop()removes the top pair; the pair below it already holds the older minimum.top()returns the value of the top pair, andget_min()returns its stored minimum.
Python solution
class MinStack:
def __init__(self):
self.items = [] # (value, min of stack so far)
def push(self, x):
cur_min = x if not self.items else min(x, self.items[-1][1])
self.items.append((x, cur_min))
def pop(self):
return self.items.pop()[0]
def top(self):
return self.items[-1][0]
def get_min(self):
return self.items[-1][1]Complexity
Each operation touches only the top of the list. Storing a minimum with every element doubles the memory but keeps it O(n).
Common mistakes
- Keeping a single
minvariable, which goes stale the moment the minimum is popped. - Using a separate min-stack but only pushing strictly smaller values, so with duplicates such as two 1s, popping one 1 loses the other.
- Calling
min(self.items)insideget_min, which silently turns it into an O(n) operation.
Frequently asked questions
What is the time and space complexity of a Min Stack?
All four operations are O(1) time. Space is O(n), since each element also stores the minimum at the time it was pushed.
Can I do it with less extra memory?
A second stack that only records a value when it is less than or equal to the current minimum uses less memory on typical inputs. A trickier approach stores encoded differences to use O(1) extra space, but it is error-prone in interviews.
How would I build a Max Stack?
Use the same idea with max instead of min. Supporting popMax as well needs a heavier structure such as a doubly linked list plus a sorted container.
Why is this considered a design question?
The task is to choose what to store so every operation stays O(1). Interviewers look for the insight that each element can carry a summary of everything beneath it.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Stack & Monotonic Stack Stacks & the Monotonic Stack lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.