Min Right Shifts to Sort An Array
Reported by candidates from Uber's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Uber asked this in August 2024, and it's a rotation trick disguised as a sorting problem. You're given an array and need to find the minimum number of right shifts to sort it. The catch: this isn't about brute-force rotating and checking. It's about finding where the sorted sequence breaks, then calculating the shift distance. Candidates who try to simulate rotations waste time. StealthCoder will catch the pattern instantly if you blank on the math.
Pattern and pitfall
The core insight is that a sorted array rotated right has exactly one break point where a larger element precedes a smaller one. Count how many elements come after that break, and that's your answer. If there's no break, the array is already sorted and the answer is zero. The trick most candidates miss: you're not rotating the array in code, you're just finding the pivot position where the sequence wraps. This is a single O(n) scan. StealthCoder flags this pattern immediately so you don't accidentally code up a simulation that times out.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Min Right Shifts to Sort An Array 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as rotate array. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Uber's OA.
Uber reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Min Right Shifts to Sort An Array FAQ
Is this just 'find the rotation point' like a rotated sorted array problem?+
Yes, but inverted. Instead of finding where an element should be, you're finding how many elements from the end need to move to the front. Locate the break where arr[i] > arr[i+1], then return n - (i + 1). If no break exists, return 0.
What if the array has duplicates?+
Duplicates don't break the logic. You still scan for the single break point where a larger element is immediately followed by a smaller one. Edge case: an already-sorted array with duplicates returns 0, which is correct.
Can I solve this in one pass?+
Yes. One linear scan from index 0 to n-1, find the break point, calculate the shift count. O(n) time, O(1) space. Don't overcomplicate it with rotation simulations or sorting.
How much time should I spend on this at Uber?+
This is medium difficulty but fast to code once you see the break-point pattern. Aim for 10-15 minutes to code and test, then move on. It's not a trick question if you remember it's about finding one pivot, not rotating.
Is there a gotcha with negative numbers or zero?+
No. The comparison logic (arr[i] > arr[i+1]) works the same regardless of sign or magnitude. The only edge case is an already-sorted array, which returns 0.