Minimum Right Rotations to Strictly Descending
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure this ZipRecruiter problem hinges on is the plain array, and nothing fancier. It was reported in October 2023, and it looks harder than it is. You get an int array and need the fewest cyclic right rotations that leave it strictly descending. Return 0 if it already qualifies, -1 if no rotation works. With up to 100000 elements, you can't rotate and recheck every time. One linear scan of adjacent pairs tells you everything. If you blank on the scan logic during the OA, StealthCoder is the quiet backup running on your screen.
The problem
Return the minimum number of cyclic right rotations that makes values strictly descending. Return 0 when it already qualifies and -1 when no rotation qualifies. Function rightRotationsToDescending(values: int[]) → int Examples Example 1 values = [4,3,2,1,5] return = 1 One right rotation moves five to the front. Example 2 values = [5,4,3] return = 0 A qualifying array needs zero rotations. Constraints 0 <= values.length <= 100000 -1000000000 <= values[i] <= 1000000000
Reported by candidates. Source: FastPrep
Pattern and pitfall
A strictly descending array rotated cyclically has at most one spot where values[i] < values[i+1], counting the wraparound pair from last to first. Scan all n adjacent pairs with modulo indexing and count the ascents or equals. If the count is 0, return 0, which only happens for length 0 or 1 since the wraparound pair breaks otherwise. If more than one break exists, return -1. If exactly one break sits at index i, meaning values[i] <= values[i+1], the new first element must be values[i+1], so the right rotation count is n - (i+1). Check example 1: [4,3,2,1,5] breaks at index 3 (1 to 5), giving 5 - 4 = 1. The pitfall is the wraparound pair, and equal neighbors, which fail the strict test. Also handle [5,4,3] carefully: wraparound 3 to 5 is a break, so the count isn't zero by pair logic alone. Treat a non-wrapping descending array as the zero case first. StealthCoder can cover you if the index math slips under the clock.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Minimum Right Rotations to Strictly Descending 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Right Rotations to Strictly Descending FAQ
How hard is this ZipRecruiter OA question really?+
Easy to medium. The idea is one pass counting breaks in the descending order, including the wraparound pair. The hard part is off-by-one on the rotation count and edge cases like empty arrays, single elements, and duplicates. Once you see the single-break rule, it's about ten lines.
What's the trick to solving it in linear time?+
Don't simulate rotations. A valid rotated array has at most one position where the descending order breaks. Find that position, and the rotation count falls out as n minus the index after the break. Zero breaks or more than one break are handled as special cases.
How do duplicates change the answer?+
Strictly descending means equal neighbors count as a break. So [3,3,2] can never qualify under any rotation. When you scan, treat values[i] <= values[i+1] as a violation, not just less than. Missing this is the most common wrong answer.
What edge cases should I test before submitting?+
Test an empty array, a single element, an already descending array like [5,4,3], an array with duplicates, and one with two breaks like [1,2,1,2]. Also test the wraparound case where the largest value sits at the end, as in example 1.
How do I prepare for this in 48 hours?+
Practice rotated-array reasoning: finding the pivot, counting breaks with modulo indexing, and converting a pivot index into a rotation count. Write the solution twice from memory, then test it on empty, single, duplicate, and two-break inputs. Aim for O(n) time and O(1) space.