Interview patterns

Monotonic Stack Pattern Explained: Next Greater Element in One Pass

Updated Nov 28, 2026 · 3 min read

A monotonic stack keeps items that are still waiting for a bigger (or smaller) value, in sorted order. Each new item pops every smaller item off the top, and each popped item's answer is the new item. Every index is pushed once and popped at most once, so next greater element problems drop from O(n²) to O(n).

Monotonic Stack reel cover

Key points

  • ●Spot it when a problem asks for the next greater, next smaller or next warmer item.
  • ●Keep indexes on the stack, not values, so you can measure the gap.
  • ●A new value pops every smaller value off the top; each pop is an answer.
  • ●Push once, pop at most once: O(n) time.
  • ●Practice on 739, 496, 503, 84 and 901.
Step-by-step visual sheet: Monotonic Stack
The full visual sheet, made with Edithly.Open the interactive sheet

The clue: "next greater" or "next warmer"

You get a list of daily temperatures: 73, 74, 75, 71, 69, 72, 76, 73. For each day, how many days until a warmer one? If none comes, the answer is 0.

The slow way checks every later day for every day. Eight days means 28 pairs to compare, and a long list makes that O(n²). Whenever a problem asks for the next greater, next smaller or next warmer item, that brute force is the hint that a monotonic stack will do it in one pass.

How the pattern works

Keep a stack of days that are still waiting for a warmer day. Because a day only stays on the stack while nothing warmer has arrived, the temperatures on the stack always go down from bottom to top. That's the "monotonic" part.

When a new day arrives:

  1. While the day on top of the stack is colder than today, pop it. Today is its answer, and the wait is the gap between the two indexes.
  2. Push today. It now waits for its own warmer day.

Any day left on the stack at the end never found a warmer one, so its answer stays 0.

The Python template

def daily_temperatures(t):
    stack = []              # indexes, temperatures decreasing
    ans = [0] * len(t)
    for i, x in enumerate(t):
        while stack and t[stack[-1]] < x:
            j = stack.pop()
            ans[j] = i - j
        stack.append(i)
    return ans

The stack holds indexes, not temperatures. You can still read the temperature with t[j], and the index gives you the distance i - j for free.

Trace it on the example

Day Temp Pops (answer) Stack after (temps)
0 73 none 73
1 74 73 (1) 74
2 75 74 (1) 75
3 71 none 75, 71
4 69 none 75, 71, 69
5 72 69 (1), 71 (2) 75, 72
6 76 72 (1), 75 (4) 76
7 73 none 76, 73

The answer is 1, 1, 4, 2, 1, 1, 0, 0. Two moments do most of the work, and the visual sheet above highlights both: when 72 arrives it pops 69 and 71, and when 76 arrives the 75 finally gets its answer after waiting 4 days. 76 and 73 are left on the stack, so they stay 0.

Why it's O(n)

The while inside the for looks like O(n²), but count the operations per index instead. Each index is pushed exactly once and popped at most once. Across the whole run that's at most 2n stack operations, so the time is O(n), with O(n) extra space for the stack and the answer.

Problems to practice

  • 739 Daily Temperatures: the template above, unchanged.
  • 496 Next Greater Element I: the same pops, recorded for the values you're asked about.
  • 503 Next Greater Element II: the same idea on a circular array.
  • 84 Largest Rectangle in Histogram: the hardest of the set, built on the same push and pop.
  • 901 Online Stock Span: the stack runs as prices stream in one at a time.

The trap to watch for

Getting the comparison backwards. t[stack[-1]] < x finds the next greater value. Flip it to > and you get the next smaller value instead. Decide which one the problem wants before you write the loop, and check whether equal values should pop or stay, since < and <= give different answers when temperatures repeat.

Frequently asked questions

What is a monotonic stack?

A stack whose values stay in one order, always decreasing or always increasing, from bottom to top. Before you push a new value, you pop anything that would break the order, and those pops are where the answers come from.

Why is a monotonic stack O(n) when it has a nested while loop?

Each index is pushed once and popped at most once. The while loop can run many times on one step, but across the whole array it pops at most n times in total, so the total work is O(n).

How do I know when to use a monotonic stack?

Look for next greater element, next smaller element, how many days until a warmer temperature, largest rectangle in a histogram or stock span. Another clue is a brute force that scans to the right for every item.

Should the stack hold values or indexes?

Usually indexes. You can always look up the value with t[i], and the index lets you compute distances like i - j, which is exactly what Daily Temperatures asks for.

Turn any problem or document into a visual sheet like this one

Paste a LeetCode problem, a PDF or a link into Edithly and get a step-by-step sheet you can study, share or quiz yourself on.

Make this sheet in Edithly, free