Monotonic stacks and the nearest greater element

← Back to Blog & Articles

A stack is the simplest data structure in this book — push, pop, peek, and you’re done — but it shows up disproportionately often in interviews because “last in, first out” is exactly the access pattern behind an enormous range of real problems: matching nested delimiters, evaluating expressions written without operator precedence ambiguity, and — the pattern that gives this discussion its second half — finding, for every element in a sequence, the nearest other element that beats it by some comparison.

A stack is the simplest data structure in this book — push, pop, peek, and you’re done — but it shows up disproportionately often in interviews because “last in, first out” is exactly the access pattern behind an enormous range of real problems: matching nested delimiters, evaluating expressions written without operator precedence ambiguity, and — the pattern that gives this article its second half — finding, for every element in a sequence, the nearest other element that beats it by some comparison. That last use case, the monotonic stack, looks unfamiliar the first time you meet it and then becomes one of the most reliably useful tricks in your toolkit once it clicks.

Reach for a plain stack when a problem involves nested or paired structure — brackets, tags, parenthesized expressions — or when you need to process tokens in an order where the most recently seen unresolved item is always the next one that matters, which is exactly what postfix expression evaluation needs. Reach specifically for a monotonic stack — one you maintain in strictly increasing or strictly decreasing order by evicting elements that violate the ordering as you go — when a problem asks something shaped like “for each element, find the next element to its right (or left) that is greater than it,” “how far away is the next larger value,” or “what’s the largest rectangle you can fit under this skyline,” because all three of those are really the same underlying question asked in different costumes. The tell for a monotonic stack specifically, as opposed to a nested-loop comparison, is that you’d otherwise be tempted to write an O(n²) scan comparing every element to every element after it; a monotonic stack does the same comparisons but throws away irrelevant candidates early, bringing the work down to O(n) because each element is pushed and popped at most once.

Stacks are the wrong tool when order doesn’t matter, when you need random access into the middle of the sequence, or when the relationship you’re tracking isn’t nested or monotonic — a problem that asks about all pairs satisfying some condition, rather than the nearest qualifying element per position, usually needs a different structure entirely, such as a hash map or a two-pointer sweep.

Continue with the book

Explore the full treatment, worked examples, and exercises in Algorithmic Edge.


Companion article for Algorithmic Edge, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.

Scroll to Top