PATTERN 8: Monotonic Stack (Next Greater / Smaller Logic)
D
Qubits of DPK
April 11, 2026
Core DSA
Core Idea (lock this sentence)
“Maintain a stack that preserves an order so I can answer nearest greater/smaller questions efficiently.”
You’re no longer using stack as just LIFO.
You’re using it as a memory of unresolved elements.
How to Recognize This Pattern (CRITICAL)
Think Monotonic Stack when you see:
- “Next greater / smaller”
- “Nearest to left / right”
- Histogram / skyline / bars
- Contribution of each element
- Brute force is clearly O(N²)
If you’re scanning left and right for each element →
If you’re resolving elements when a better one appears →
30 MUST-DO INTERVIEW PROBLEMS
(10 Easy → 10 Medium → 10 Hard)
EASY (1–10): Stack + Intuition Building
Goal: understand why elements are popped
- #Next Greater Element I – LC 496
- #Next Greater Element II – LC 503
- #Daily Temperatures – LC 739
- #Final Prices With a Special Discount – LC 1475
- #Online Stock Span – LC 901
- #Buildings With an Ocean View – LC 1762
- #Number of Visible People in a Queue – LC 1944
- #Maximum Width Ramp – LC 962
- #Longest Valid Parentheses – LC 32
- #Car Fleet – LC 853
After these:
You should
️ MEDIUM (11–20): Real Interview Stack Logic
Write English explanation before code.
- #Largest Rectangle in Histogram – LC 84
- #Trapping Rain Water (Stack) – LC 42
- #Decode String – LC 394
- #Validate Stack Sequences – LC 946
- #Asteroid Collision – LC 735
- #Remove Duplicate Letters – LC 316
- #Maximum Score of a Good Subarray – LC 1793
- #Shortest Unsorted Continuous Subarray (Stack view) – LC 581
- #Next Greater Node in Linked List – LC 1019
- #Most Competitive Subsequence – LC 1673
Interviewers listen for:
“Why am I popping now?”
HARD (21–30): Contribution & Boundary Thinking
These test deep stack intuition.
- #Maximal Rectangle – LC 85
- #Trapping Rain Water II – LC 407
- #Count Submatrices With All Ones – LC 1504
- #Number of Valid Subarrays – LC 1063
- #Maximum Subarray Min-Product – LC 1856
- #Sum of Total Strength of Wizards – LC 2281
- #Sum of Subarray Minimums – LC 907
- #Minimum Cost Tree From Leaf Values – LC 1130
- #Create Maximum Number – LC 321
- #Longest Valid Parentheses (Stack Hard) – LC 32
These require:
- Understanding element contribution
- Correct boundary resolution
- Zero fear of stack mechanics
The Golden Rule (Never Forget)
For every element, ask:
“Am I still useful, or did someone better arrive?”
If better arrived → I pop.
That is monotonic stack thinking.
️ Final Advice
When solving:
- Always clarify: Increasing stack or Decreasing stack?
- Are duplicates allowed?
- Strict or non-strict comparison?
- Do I store index or value?
Interviewers love these clarifications.
Common Interview Killers
- Pushing everything blindly
- Forgetting indices vs values
- Not knowing whether stack is increasing or decreasing
- Explaining code without explaining why pops happen
INTERVIEW REVISION CARD — Pattern 8
1️⃣ Pattern in One Line
"Maintain a stack of unresolved elements — pop when a better candidate arrives."
2️⃣ Recognize It When...
- "Next greater/smaller element" to left or right
- Histogram, bars, skyline problems
- Contribution of each element to all subarrays
- Trigger words: "nearest", "next greater", "bars", "span", "rectangle"
3️⃣ Don't Confuse With...
- Sliding Window (P4) → SW maintains a window condition; monotonic stack resolves elements
- Heap (P12) → heap gives global min/max; stack gives local nearest greater/smaller
- DP (P13) → DP stores all states; stack discards irrelevant elements
4️⃣ Approach in 3 Steps
- #Decide: increasing stack (find next smaller) or decreasing stack (find next greater)?
- #For each element: pop all elements that are now "resolved" by current element
- #Store index in stack (not value) — you need distance calculations
5️⃣ Invariant to Maintain
"At every step, elements in the stack are still waiting for their next greater/smaller — none have been resolved yet."
6️⃣ Complexity to Justify
- Time: O(N) — each element pushed and popped at most once
- Space: O(N) — stack holds at most N elements
- Why: Total push + pop operations = 2N
7️⃣ Edge Cases to Always Check
- Empty stack before popping
- Duplicate elements (strict vs non-strict comparison)
- No greater/smaller element exists (stack never empty)
- Single element array
- All elements same
8️⃣ One Line to Say in Interview
"I'll use a monotonic stack — elements are popped when a better candidate arrives, giving O(N) time since each element is pushed and popped at most once."