Reported October 2024
Amazonsorting

Sort Permutation

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

Amazon's Sort Permutation question hits your OA in October, and it's a sorting problem that looks simpler than it is. You're given a permutation and need to sort it into natural order, but the real trick is figuring out what the assessment actually wants from you: are you counting swaps, returning indices, or reconstructing the sorted order. The pattern is sorting, but the gotcha is understanding the specific output format before you start coding. StealthCoder can read the exact problem text on screen and flag the output requirement in real time.

Pattern and pitfall

Sort Permutation is fundamentally about permutations and cycles. A permutation can be decomposed into cycles, and the minimum number of swaps needed to sort it equals n minus the number of cycles. If the problem asks for swap count, you build a union-find or DFS to identify cycles. If it asks for the sorted array itself, you just return the natural order [1, 2, 3,...]. The common pitfall is assuming you need to simulate the actual swaps when you don't. Amazon often tests whether you recognize the mathematical property rather than brute-force the answer. StealthCoder gives you the safety net here: if you freeze on the approach, you'll see the pattern clearly on the OA platform itself.

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 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 StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as permutation sequence. If you have time before the OA, drill that.

⏵ 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 Permutation FAQ

Is this asking for the number of swaps or the sorted array?+

Read the output specification carefully. Amazon often asks for swap count using the cycle decomposition method (n - number of cycles). If it asks for the sorted array, that's just [1, 2, 3,...]. The problem text will be explicit. Don't assume.

What's the algorithmic trick?+

Recognize that every permutation can be broken into disjoint cycles. Use union-find or DFS to count cycles. Minimum swaps = array length minus cycle count. This is O(n) once you identify the cycles, much faster than simulating swaps.

How do I identify cycles in a permutation?+

Start at an unvisited index, follow where it points, mark visited nodes. When you return to the start, you've found one cycle. Repeat for all unvisited indices. The number of cycles tells you the swap count directly.

Is this still asked by Amazon in 2024?+

Yes, reported October 2024. Amazon tests sorting and permutation properties regularly. The cycle decomposition approach is the expected solution for mid-level roles.

How do I prepare in 48 hours?+

Understand cycle decomposition in permutations. Write code to count cycles once. Practice on one or two permutation problems. The pattern is consistent. You're not memorizing; you're recognizing the cycle property under pressure.

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