4 views
# Monotonic Stack Next Greater (Practice 4504): Notes Interviewers love monotonic stack next greater because it checks whether you can state the invariant before coding. Here is a compact rehearsal note. ## Approach Pop smaller tops; current is their next greater. ```python def next_greater(nums): n = len(nums); ans = [-1]*n; st = [] for i, x in enumerate(nums): while st and nums[st[-1]] < x: ans[st.pop()] = x st.append(i) return ans ``` ## Complexity Linear in the dominant input size for the scan; heap/tree variants add a log factor. ## Interview pitfalls State the invariant out loud, name the complexity, then code. Skipping the invariant is the usual fail mode. ## Practicing out loud Narrate 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 [undetectable AI interview copilot](https://www.linkjob.ai/interview-copilot/).