Reported July 2026
Amazonarray

Sort an Array with Rotate and Flip

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

The mistake that sinks most first attempts at this Amazon OA question, reported in July 2026, is simulating it. People write a BFS over rotate and flip states, and with 200000 elements it dies on time. The question is really a circular-order check dressed up as an operations puzzle. Rotations keep the circular order and flips reverse it, so you only need to know whether the array is a circular rotation of ascending or descending order, then count cheap moves. If you're taking this in a day or two, learn that one idea cold. StealthCoder is the safety net running invisibly during the live OA if your head goes blank on the edge cases.

The problem

You are given an array values containing distinct integers. You may apply either of these operations:
Rotate: Move the first element to the end of the array.
Flip: Reverse the entire array.
Return the minimum number of operations needed to place values in strictly increasing order. You may use the operations in any sequence. If increasing order cannot be reached, return -1.

Function
minSortOperations(values: int[]) → int

Examples
Example 1
values = [3,4,1,2]
return = 2
Rotate twice: [3,4,1,2] becomes [1,2,3,4]. No single operation produces increasing order.
Example 2
values = [3,2,1,4]
return = 2
Flip to obtain [4,1,2,3], then rotate once to obtain [1,2,3,4].
Example 3
values = [1,3,2,4]
return = -1
Rotations preserve the circular order, and a flip only reverses that order. Neither orientation can match [1,2,3,4], so sorting is impossible.

Constraints
1 <= values.length <= 200000
Every element is a 32-bit signed integer.
All elements are distinct.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Count circular descents. Exactly one descent (a[i] > a[(i+1) mod n]) means the array is a rotation of ascending order. Let k be the index of the minimum. Cost is min(k, n-k+2). Left rotate k times, or flip, rotate n-k, flip, which acts as a right rotation. Exactly one circular ascent means descending rotation. Let j be the index of the maximum. Cost is min(j, n-j)+1, since one flip plus rotations in whichever direction is cheaper. If neither holds, return -1. The pitfall is forgetting the two-flip trick for right rotation, which gives wrong answers when k is large. Also handle n=1 and n=2, where both shapes fit, so take the smaller cost. This runs in O(n). StealthCoder is the hedge in the live OA if you can't recall the direction logic under pressure.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Sort an Array with Rotate and Flip 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 StealthCoder

Related leaked OAs

⏵ The honest play

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.

Sort an Array with Rotate and Flip FAQ

What's the trick in the Amazon rotate and flip sorting question?+

Treat the array as a circle. Rotations never change circular order and a flip only reverses it. So sorting is possible only if the circle is already ascending or descending. Then it's pure arithmetic on the index of the minimum or maximum, with no simulation needed.

How do I know when to return -1?+

Count circular descents, comparing the last element to the first too. If there's exactly one, it's an ascending rotation. If there's exactly one circular ascent, it's a descending rotation. Anything else, like [1,3,2,4], can't be fixed by rotate and flip, so return -1.

Why is the ascending cost min(k, n-k+2) and not just k?+

Left rotating k times is one option. But flip, rotate n-k times, flip back is a net right rotation by n-k, costing n-k+2. When k is large, that's cheaper. Missing this gives wrong answers on tests where the minimum sits near the end.

Will a brute-force BFS pass with 200000 elements?+

No. The state space is huge and each state costs O(n) to copy or compare. The constraints are a hint that you need an O(n) scan. Find the minimum or maximum index, check the circular order once, and compute the answer with a formula.

How do I prepare for this in 48 hours?+

Write the solution once from scratch. Test [3,4,1,2], [3,2,1,4], [1,3,2,4], a single element, and a two-element array. Those cover rotation, flip, impossible, and the tie where both orientations fit. If you can explain why the formulas work, you're ready for variants.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it