As reported: ServiceNow Software Engineer (IC2) loop, round 1 (live technical in a shared editor, JavaScript), Jun 2025: for each element print its next greater element to the right or
-1; the candidate gave theO(n²)brute force first, then a monotonic stack inO(n), and covered decreasing arrays and duplicates.
Given an integer array nums, return an array result where result[i] is the first element to the right of index i that is strictly greater than nums[i], or -1 if there is none.
This is the single-array form of LeetCode 496 — Next Greater Element I.
Example
[11,13,21,3] -> [13,21,-1,-1]
[4,5,2,25] -> [5,25,25,-1]
Constraints: 1 ≤ nums.length ≤ 10^5; -10^9 ≤ nums[i] ≤ 10^9.
Code runner format: one line with nums. Implement nextGreater(nums); the harness prints the result array.
Clarifications
- "Greater" is strict: an equal value to the right does not count (
[2,2,2] -> [-1,-1,-1]).