Minimum Operations to Sort a Permutation
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Amazon OA reported in January 2026 hides a trap in plain sight: the array is already sorted, or it's sorted after one reverse, and a naive simulation walks right past the cheapest answer. If you're taking this one soon, know the shape. It's a permutation, two operations (rotate left by one, reverse), and a guarantee that it's solvable. That means the array is a rotation of the sorted order or a rotation of the descending order. No search needed. StealthCoder sits as a quiet safety net during the live OA if your mind goes blank on the case analysis, but the logic below is short enough to carry in your head.
The problem
You are given a permutation arr of size n, containing each integer from 1 to n exactly once. In one operation, you may do either of the following: Move the first element of the array to the end, shifting every other element one position to the left. Reverse the entire array. It is guaranteed that the array can be sorted into increasing order using these operations. Return the minimum number of operations needed to sort arr. Function minOperationsToSortPermutation(arr: int[]) → int Examples Example 1 arr = [3,1,2] return = 1 Move the first element 3 to the end to get [1,2,3]. Example 2 arr = [1,5,4,3,2] return = 2 Move 1 to the end to get [5,4,3,2,1], then reverse the array to get [1,2,3,4,5]. Example 3 arr = [1,2,3,4] return = 0 The array is already sorted. Constraints 1 <= arr.length <= 10^5 1 <= arr[i] <= arr.length arr is a permutation of integers from 1 to arr.length. It is guaranteed that arr can be sorted using the given operations.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Don't simulate. With n up to 10^5, BFS over states is dead on arrival. Think in terms of structure. Since the array is solvable, it's either a rotation of [1..n] or a rotation of [n..1]. For the ascending case, find the index i of value 1. Rotating left i times sorts it, so the cost is i. For the descending case, find the index j of value n. Rotating left j times gives descending, then one reverse, so the cost is j + 1. But reversing first can also help: reverse, then rotate. Compute the candidates and take the minimum. The pitfall is the already-sorted array and the fully reversed array, which cost 0 and 1. Also watch n = 1 and n = 2, where both patterns overlap. Check the ascending rotation first, then descending, and return the smaller cost. If you freeze mid-assessment, StealthCoder can hand you the working solution, but you should still verify against the three examples.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Minimum Operations to Sort a Permutation 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 Amazon's OA.
Amazon 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 Operations to Sort a Permutation FAQ
What's the trick in Minimum Operations to Sort a Permutation?+
Skip simulation. The guarantee means the array is a rotation of ascending or descending order. Locate where 1 or n sits, count the left rotations needed, add one reverse for the descending case, and compare against reversing first. It's O(n) with no extra structure.
How hard is this Amazon OA question really?+
Easier than it looks. The statement sounds like a shortest-path search, but it collapses to index arithmetic. The difficulty is spotting that, then handling edge cases like already sorted input, fully reversed input, and tiny arrays.
What edge cases break a naive solution?+
An already sorted array must return 0. A fully descending array must return 1, not a rotation count. Arrays of length 1 or 2 fit both patterns at once, so compute both candidates and take the minimum instead of picking one.
Why not BFS over array states?+
With length up to 10^5, the state space is far too large and each state costs O(n) to copy. You'd time out or run out of memory. The solvability guarantee exists so you can reason about structure and skip search.
How do I prepare for this in 48 hours?+
Practice rotation and reversal index math on small arrays by hand. Work the three given examples until the formulas feel automatic. Then write the O(n) solution once from scratch and test already sorted, reversed, and length 1 inputs.