1# Monotonic Stack Next Greater (Practice 4399): Walkthrough
2
3Interviewers love monotonic stack next greater because it checks whether you can state the invariant before coding. Here is a compact rehearsal note.
4
5## Approach
6
7Pop smaller tops; current is their next greater.
8
9```python
10def next_greater(nums):
11 n = len(nums); ans = [-1]*n; st = []
12 for i, x in enumerate(nums):
13 while st and nums[st[-1]] < x:
14 ans[st.pop()] = x
15 st.append(i)
16 return ans
17```
18
19## Complexity
20
21Linear in the dominant input size for the scan; heap/tree variants add a log factor.
22
23## Interview pitfalls
24
25State the invariant out loud, name the complexity, then code. Skipping the invariant is the usual fail mode.
26
27## Practicing out loud
28
29Narrate the approach, complexity and one edge case in under a minute. Disclosure: I work on Linkjob AI, a real-time interview assistant. If you want live practice help, see [www.linkjob.ai](https://www.linkjob.ai/).
Created: Sat, 07 Nov 2026 22:31:06 +0000
Expires: Never