Coding interview problems โบ Stack & Monotonic Stack
๐ Stack & Monotonic Stack Easy
Next Greater Element
Next Greater Element explained: a monotonic stack finds the first larger value to the right of every element in O(n). Tested Python, pitfalls and a live visual.
Also searched as: next greater element using stack ยท NGE problem ยท next larger element to the right
Try it live Free
Stacks & the Monotonic Stack
Open the monotonic stack mode and step through: each waiting element pops off the instant a bigger one arrives, which is the moment its next greater element is found.
The problem
For each element of a list of integers, find the first element to its right that is strictly larger. Return a list of those values, using -1 where no larger element exists.
The key insight
Elements that have not found their answer yet sit on a stack, and their values are decreasing from bottom to top. A new value is the answer for every smaller element on top of the stack, so pop those and record it. Each element waits on the stack exactly until its answer arrives.
Step-by-step approach
- Create
result = [-1] * nand an empty stack of indices. - Scan the list left to right with index
i. - While the stack is not empty and
nums[stack[-1]] < nums[i], pop indexjand setresult[j] = nums[i]. - Push
i. - Anything still on the stack at the end has no greater element and keeps
-1.
Python solution
def next_greater(nums):
result = [-1] * len(nums)
stack = [] # indices still waiting for an answer
for i, x in enumerate(nums):
while stack and nums[stack[-1]] < x:
result[stack.pop()] = x # x is the first larger value to the right
stack.append(i)
return resultComplexity
Each index enters and leaves the stack at most once, so the total work across all iterations of the inner loop is O(n).
Common mistakes
- Popping on
<=, which makes an equal value count as greater. - Pushing values instead of indices, which breaks when you need to write the answer back to the right position, especially with duplicates.
- Forgetting the circular variant: if the list wraps around, you must scan it twice (indices 0 to 2n-1, using
i % n).
Frequently asked questions
What is the time and space complexity of Next Greater Element?
O(n) time, since each element is pushed and popped at most once, and O(n) space for the stack.
How do I handle a circular array?
Loop i from 0 to 2n - 1 and use nums[i % n], but only push indices during the first pass. Elements near the end can then find a greater element at the start.
How do I find the next smaller element instead?
Flip the comparison: pop while the top is greater than the current value. The stack then keeps increasing values.
What about the previous greater element?
Scan the same way, but before pushing i, the element on top of the stack (after popping smaller ones) is the previous greater element of i.
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.