Next Greater Element I
A easy-tier problem at 75% community acceptance, tagged with Array, Hash Table, Stack. Reported in interviews at Swiggy and 9 others.
Next Greater Element I shows up in OAs at Amazon, Microsoft, Goldman Sachs, and Bloomberg. It looks trivial at first: given two arrays, find the next greater element in the second for each element in the first. Most candidates code a nested loop, hit the time limit, and panic. The trick is a monotonic stack. It's not hard once you see it, but if you blank on the pattern during the live assessment, StealthCoder surfaces the solution invisibly and you move on.
Companies that ask "Next Greater Element I"
Next Greater Element I is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by a senior engineer who knows the OA is theater. This is the script.
Get StealthCoderThe naive O(n*m) nested loop works for small inputs but fails at scale. The real pattern is a monotonic decreasing stack paired with a hash table. Iterate the second array once, maintain a stack of elements in decreasing order, and pop when you find a greater element. Store results in a map. Time drops to O(n+m). The insight is counterintuitive: you're building answers as you go forward, not searching backward. Most candidates miss this because they think linearly. Hash Table and Monotonic Stack are the core topics here. Stack problems often hide a single aha moment. StealthCoder's edge on this one is showing you the stack construction in real time if the pattern doesn't click during your OA.
Pattern tags
You know the problem.
Make sure you actually pass it.
Next Greater Element I recycles across companies for a reason. It's easy-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by a senior engineer who knows the OA is theater. This is the script. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Next Greater Element I interview FAQ
Is this still asked at FAANG?+
Yes. Amazon, Microsoft, Goldman Sachs, and Bloomberg all report it. Acceptance rate is 74%, so it's considered accessible, but that doesn't mean candidates nail it on first try. The nested-loop trap catches a lot of people.
What's the actual trick?+
Monotonic stack. Iterate the second array, maintain a decreasing stack, pop when you find a greater element, and store the result in a hash table. Build answers forward, not backward. It's O(n+m) time, O(n) space.
Why does the nested loop fail?+
It's O(n*m). For large arrays it hits time limits. The stack approach processes each element once by using a data structure that remembers what you've seen. Stack operations are O(1).
How is this related to the other stack topics?+
Monotonic Stack is the central pattern. Hash Table is the output store. Array is the input. The problem teaches you how stacks maintain invariants to solve range-query problems efficiently.
Is this harder or easier than it looks?+
Easier once you see the monotonic stack pattern. Harder if you're not familiar with that idiom. 74% acceptance suggests it's not a gotcha problem, but the conceptual leap from nested loop to stack isn't trivial for unprepared candidates.
Want the actual problem statement? View "Next Greater Element I" on LeetCode →