{"content":"# Monotonic Stack Next Greater (Practice 4294): Scan\n\nInterviewers love monotonic stack next greater because it checks whether you can state the invariant before coding. Here is a compact rehearsal note.\n\n## Approach\n\nPop smaller tops; current is their next greater.\n\n```python\ndef next_greater(nums):\n    n = len(nums); ans = [-1]*n; st = []\n    for i, x in enumerate(nums):\n        while st and nums[st[-1]] < x:\n            ans[st.pop()] = x\n        st.append(i)\n    return ans\n```\n\n## Complexity\n\nLinear in the dominant input size for the scan; heap/tree variants add a log factor.\n\n## Interview pitfalls\n\nState the invariant out loud, name the complexity, then code. Skipping the invariant is the usual fail mode.\n\n## Practicing out loud\n\nNarrate 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 [real-time interview help](https://www.linkjob.ai/interview-copilot/).\n"}