Next Greater Element to the Right
Reported by candidates from Tekion's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Tekion reported this one in September 2026, and the input size is the whole story. With nums.length up to 100000, the obvious scan-right-from-every-index loop turns into roughly ten billion comparisons in the worst case. That's the wall. The OA wants a monotonic stack that finishes in one pass, and it's a clean, short problem if you've seen the shape before. If you haven't, or you blank under the timer, StealthCoder runs invisibly on your screen during the live assessment and hands you the stack solution so you can still ship it. Here's the trick, the pitfall, and what to expect.
The problem
Given an integer array nums, return an array answer of the same length. For each index i, answer[i] is the first value strictly greater than nums[i] at an index to its right. If no strictly greater value exists to the right, set answer[i] to -1. The array is not circular. Function nextGreaterElements(nums: int[]) → int[] Examples Example 1 nums = [2,1,2,4,3] return = [4,2,4,-1,-1] The first greater values to the right are 4, 2, and 4. The final two positions have none. Example 2 nums = [1,3,2,4] return = [3,4,4,-1] Each answer uses the nearest later value that is strictly larger. Example 3 nums = [5] return = [-1] The only element has no value to its right. Constraints 1 <= nums.length <= 100000. -10^9 <= nums[i] <= 10^9.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The pattern is a monotonic stack. Walk the array once, keeping a stack of indices whose next greater value hasn't been found yet. For each new value, pop every index whose value is strictly less than the current one and set its answer to the current value. Then push the current index. Whatever remains on the stack at the end gets -1. Each index is pushed and popped at most once, so it's O(n) time and O(n) space. The classic pitfall is using greater-or-equal when popping. The problem says strictly greater, so equal values must stay on the stack. In Example 1, the first 2 gets 4, not the later 2. Another trap is storing values instead of indices, which breaks the answer writes. Initialize the answer array to -1 up front. If your mind goes blank mid-assessment, StealthCoder is the hedge that gets you unstuck.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Next Greater Element to the Right cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Tekion's OA.
Tekion reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Next Greater Element to the Right FAQ
How hard is the Tekion next greater element question really?+
It's a standard medium-easy problem if you know monotonic stacks. The logic is about ten lines. The difficulty is recognizing that brute force is too slow at 100000 elements and that a single pass with a stack fixes it.
What's the trick to solving it fast?+
Keep a stack of indices with decreasing values. When the current number is larger than the value at the stack top, pop and record the current number as that index's answer. Push the current index after. Leftovers stay at -1.
Why does brute force fail here?+
The array can hold 100000 elements. Checking every element to the right of each index is O(n squared), which is about ten billion operations in the worst case, such as a descending array. A one-pass stack solution is O(n).
Do I pop on greater-than or greater-than-or-equal?+
Pop only when the current value is strictly greater than the value at the stack top. Equal values must not resolve each other. Example 1 shows it: the first 2 resolves to 4, not to the second 2.
How do I prepare for this in 48 hours?+
Write the stack solution from scratch twice, then test it on descending arrays, all-equal arrays, a single element, and negative values. Then try the circular variant so you see how the pattern changes. That covers most of what this OA could throw at you.