Next Greater Element to the Right
Updated
Problem statement
For every element in nums, find the first element to its right that is strictly greater. Return an array of those greater values in the same index order. If no greater value exists to the right of a position, return -1 there.
For this exercise, assume equal values do not qualify, negative values are permitted, and an empty input returns an empty array. Return the nearest qualifying value, rather than the largest value anywhere to the right.
Function
nextGreaterValues(nums: int[]) → int[]Examples
Example 1
nums = [4,2,5,1,3]return = [5,5,-1,3,-1]The first greater value after both 4 and 2 is 5; after 1 it is 3. Values 5 and 3 have no greater value to their right.
Example 2
nums = [2,2,1]return = [-1,-1,-1]An equal 2 does not qualify as greater, and all later values are smaller.
Constraints
- For this exercise, assume
0 <= nums.length <= 10^5. -10^9 <= nums[i] <= 10^9.
More PeopleStrong Onsite Interview questions
- Add Two Binary StringsONSITE INTERVIEW · Seen Mar 2026
- Four SumONSITE INTERVIEW · Seen Mar 2026
- LRU Cache OperationsONSITE INTERVIEW · Seen Mar 2026
- Sliding Window MaximumONSITE INTERVIEW · Seen Mar 2026
- Find the Entry of a Linked List CycleONSITE INTERVIEW · Seen Sep 2025
- Integer to English Words: Up to 20 DigitsONSITE INTERVIEW · Seen Sep 2025
- Word Ladder II: All Shortest TransformationsONSITE INTERVIEW · Seen Sep 2025
- Detect a Cycle in a Linked ListONSITE INTERVIEW · Seen May 2025
- First Nonrepeating CharacterONSITE INTERVIEW · Seen May 2025
- Longest Subarray with a Target SumONSITE INTERVIEW · Seen May 2025
Similar questions at other companies
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Fountain SafetyGoogle · ONSITE INTERVIEWGoogle interview questions
- Trace Events from Stable Whole-Stack StatesAnthropic · ONSITE INTERVIEWAnthropic interview questions
- Next Smaller Ticket PriceAmazon · ONSITE INTERVIEW
- Next Greater Element to the RightTekion · ONSITE INTERVIEWTekion interview questions
- Surviving Balls after Directional CollisionsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Daily TemperatureseBay · ONSITE INTERVIEWeBay interview questions
- Asteroid CollisionAdobe · ONSITE INTERVIEWAdobe interview questions
- Most Frequent Call Path in Interleaved LogsRoblox · ONSITE INTERVIEWRoblox interview questions