Python 3.11+
The examples use the standard Python syntax and containers supported by the browser Judge; the Monotonic Stack invariant is independent of a minor Python release.
Verify in Python docs(opens in a new tab)Maintain ordered unresolved candidates for next-greater, next-smaller, and span problems. Learn its decision rule, maintained state, Python cost model, and transfer from a focused drill to a full Interview Problem.
Recognize it when
Consider Monotonic Stack when the prompt's constraints and required operations match this shape: Maintain ordered unresolved candidates for next-greater, next-smaller, and span problems.

Sign in to save your code and progress across devices. The lesson and problem statement remain public.
A monotonic stack stores candidates whose next qualifying element has not appeared. Its value order lets one new item resolve a suffix of weaker candidates at once.
For next greater, pop while the current value is strictly greater than the top. The current index is the first qualifying answer because every earlier processed value failed to pop that candidate.
def increasing_stack_trace(values):
stack=[]
trace=[]
for index,value in enumerate(values):
popped=[]
while stack and values[stack[-1]] < value:
popped.append(stack.pop())
stack.append(index)
trace.append([index,popped,stack.copy()])
return traceImplement increasing_stack_trace(values). Return [index, popped_indexes, stack_indexes] for each value while maintaining an increasing stack.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Store indexes when the answer needs distance, position, or access to parallel data. Store values only when position is irrelevant. Decide strict versus non-strict comparison explicitly so duplicates have correct ownership.
def next_greater_distance(values):
result=[0]*len(values)
stack=[]
for index,value in enumerate(values):
while stack and values[stack[-1]] < value:
previous=stack.pop()
result[previous]=index-previous
stack.append(index)
return resultImplement next_greater_distance(values). Return distance to the next strictly greater value for each index, or 0.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Use a monotonic stack for nearest qualifying neighbors in scan order. Use a heap for global best candidates where resolution order follows priority rather than adjacency. Every index is pushed and popped at most once, giving O(n) time and O(n) space.
Say: “The stack is monotone and contains only unresolved indexes. This current value is the first one able to resolve each popped index.” Then explain why remaining stack entries legitimately receive the sentinel.
Python 3.11+
The examples use the standard Python syntax and containers supported by the browser Judge; the Monotonic Stack invariant is independent of a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| Monotonic Stack decision loop | O(n) | O(n) | Nearest earlier elements under an ordering constraint can discard candidates permanently as soon as a later value dominates them. Stack indices increase from bottom to top and their values are strictly increasing after invalid candidates are removed. |
O(n) for the focused Maintain a Monotonic Stack implementation.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
Applying the pattern without proving its ordering, monotonicity, or window invariant can silently miss valid candidates.
Prevent it: Write the decision rule beside the loop and verify it against Maintain a Monotonic Stack before optimizing.
Avoid
def previous_smaller_indices(values):
passUse instead
def previous_smaller_indices(values):
stack = []
result = []
for index, value in enumerate(values):
while stack and values[stack[-1]] >= value:
stack.pop()
result.append(stack[-1] if stack else -1)
stack.append(index)
return resultWhere you will hit this: Maintain a Monotonic Stack(opens in a new tab)
Keeps equal-valued candidates and reports them even though the predecessor must be strictly smaller.
Prevent it: Use the public tests and preserve this state: Stack indexes remain unresolved and monotone by value.
Avoid
def previous_smaller_indices(values):
stack = []
result = []
for index, value in enumerate(values):
while stack and values[stack[-1]] > value:
stack.pop()
result.append(stack[-1] if stack else -1)
stack.append(index)
return resultUse instead
def previous_smaller_indices(values):
stack = []
result = []
for index, value in enumerate(values):
while stack and values[stack[-1]] >= value:
stack.pop()
result.append(stack[-1] if stack else -1)
stack.append(index)
return resultWhere you will hit this: Maintain a Monotonic Stack(opens in a new tab)
Python slicing, copying, membership in lists, or repeated sorting inside the loop can invalidate the intended complexity.
Prevent it: Count every slice, copy, sort, membership check, and container update before claiming O(n).
Avoid
def previous_smaller_indices(values):
stack = []
result = []
for index, value in enumerate(values):
while stack and values[stack[-1]] > value:
stack.pop()
result.append(stack[-1] if stack else -1)
stack.append(index)
return resultUse instead
def previous_smaller_indices(values):
stack = []
result = []
for index, value in enumerate(values):
while stack and values[stack[-1]] >= value:
stack.pop()
result.append(stack[-1] if stack else -1)
stack.append(index)
return resultWhere you will hit this: Car Fleet(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27