Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
PROBLEM-SOLVING TECHNIQUES

Monotonic Stack

A stack kept in sorted order to find the next greater or smaller element.

Advanced8 problems3 interactive subtopicsO(n) time · O(n) space
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (3)Open in full playground →
STEP 1/5Each day is warmer than the last
74 pops 73, then 75 pops 74
0123456
73
74
75
71
69
72
76
i
answer
1
0
1
1
0
2
0
3
0
4
0
5
0
6
STACK - TOP AT THE TOP
75 · i2
popped
73 · i0
74 · i1
  • visited / done
  • active this step
  • result
  • removed / popped
Space Play←→ StepR Replay
1 / 5
PYTHONtemps = [73, 74, 75, 71, 69, 72, 76]
1def daily_temperatures(temps):
2 answer = [0] * len(temps)
3 stack = []
4 for i, t in enumerate(temps):
5 while stack and temps[stack[-1]] < t:
6 j = stack.pop()
7 answer[j] = i - j
▶8 stack.append(i)
9 return answer
STEP 1 OF 5Setup

Each day is warmer than the last

74 pops 73 (1 day), and 75 pops 74 (1 day). The stack holds 75’s index.

VARIABLES

i
2
stack
[2]
answer
[1, 1, 0, 0, 0, 0, 0]
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

Push indices onto a stack while keeping their values decreasing (or increasing). A new element pops every smaller one - and is their “next greater” answer.

Why it matters

Each index is pushed and popped once, so “next greater for every element” is O(n) instead of O(n²).

Think of it like this

People in a line looking right: a tall person arriving blocks the view of everyone shorter before them.

Use it when

  • Next greater or next smaller element
  • Span and “days until warmer” questions
  • Largest rectangle in a histogram

Avoid it when

  • You need each window’s maximum - use a monotonic queue
  • No “nearest bigger or smaller” relationship in the problem

Complexity cheatsheet

OperationTimeSpace
Monotonic stack passO(n)O(n)
Scan right from each elementO(n²)O(1)
Each push or popO(1)-

Often confused with: monotonic queues - they also drop items from the front as a window moves on.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

next greaternext warmer dayspanhistogramprevious smaller
  • Store indices, not values - you usually need distances
  • Indices left on the stack at the end have no answer
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • Storing values instead of indices, which loses the distance
  • Using <= where < is needed when values are equal
  • Forgetting that indices left on the stack keep the default answer

Edge cases to test

  • Strictly decreasing input - nothing ever pops
  • All values equal
  • A single element
Interview tip: Say “each index is pushed once and popped at most once” - that is the O(n) proof.
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

Why is a monotonic stack O(n) even with a while loop inside the for loop?

06

Practice

Apply what you just watched on real interview problems.

Generate Stack problem in AI DSA Practice →
  • BeginnerNext Greater Element ILeetCode 496 ↗ (opens in a new tab)
  • IntermediateDaily TemperaturesLeetCode 739 ↗ (opens in a new tab)
  • AdvancedLargest Rectangle in HistogramLeetCode 84 ↗ (opens in a new tab)
Merge IntervalsMonotonic Queue