Steps to the Next Higher Value in a Circle
Updated
Problem statement
You are given a list of integers values arranged in a circle: after the last position comes the first position again.
For each position i, start at i and move forward one position at a time, wrapping from the end to the start. Count how many steps it takes to reach a value that is strictly greater than values[i]. If no other position holds a greater value, the answer for i is -1.
Return an array answer of the same length, where answer[i] is the step count for position i.
Function
stepsToNextHigher(values: int[]) → int[]Examples
Example 1
values = [3,1,4,2]return = [2,1,-1,1]From 3, the next higher value is 4, two steps ahead. From 1, it is 4, one step ahead. Nothing is higher than 4, so it gets -1. From the last value 2, there is nothing to its right, so wrap around to the start: 3 is one step ahead.
Example 2
values = [5,5,5]return = [-1,-1,-1]All values are equal. An equal value is not higher, so every position gets -1.
Example 3
values = [4,9,1,3,2]return = [1,-1,1,2,1]From 3 (index 3), the value 2 one step ahead is lower, then the walk wraps to 4 at index 0, which is two steps ahead. From 2 (index 4), the wrap reaches 4 after one step. 9 is the largest value, so it gets -1.
Constraints
1 <= values.length <= 2000-10^9 <= values[i] <= 10^9
More Rippling Online Assessment questions
- Most Segments That Sort the ArrayOA · Seen Nov 2026
- Palindromic Paths to the RootOA · Seen Nov 2026
- Server InvestmentOA · Seen Nov 2026
- Corporate Card Expense RulesOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- REST API: Highest International StudentsOA · Seen Sep 2026
- Token ValidationOA · Seen Sep 2026
- Consistent LogsOA · Seen Sep 2026
- Costliest ChocolateOA · Seen Sep 2026
- Server ManagementOA · Seen Sep 2026
- Cyber Beacon DetectionOA · Seen Aug 2026
Similar questions at other companies
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Sum of Subarray MinimumsNVIDIA · OANVIDIA interview questions
- Shopkeeper Final Price SummaryMicrosoft · OAMicrosoft interview questions
- Nested Recipe Blocks (Game Adaptation)Roblox · OARoblox interview questions
- Maximum Capped Histogram Area After One RemovalMyntra · OAMyntra interview questions
- Shopkeeper Final PriceUber · OAUber interview questions
- Predict AnswerAmazon · OA
- Retention Policy CollisionBox · OABox interview questions
- Longest Prefix-Valid Binary SubarrayHackerRank · OAHackerRank interview questions
- Sum of Subarray RegionsPatreon · OA