Reported August 2020
Postmansorting

Minimum Swaps to Sort an Array

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

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

Postman reportedly asked this one in August 2020, and it looks like a sorting problem but it isn't really about sorting. It's about cycles. If you've got an OA coming up, the question is how you turn a swap count into something you can compute in one pass after a sort. Once you see the permutation hiding inside the array, the code is about fifteen lines. If you blank on the cycle idea during the live assessment, StealthCoder can sit invisibly on your screen as a safety net and hand you the approach.

The problem

Given an array values of distinct integers, return the minimum number of swaps of any two positions needed to arrange the array in strictly increasing order.

Function
minimumSwaps(values: int[]) → int

Examples
Example 1
values = [4,3,1,2]
return = 3
One optimal sequence swaps the values at positions 0 and 2, then positions 1 and 3, then positions 2 and 3, producing [1,2,3,4].
Example 2
values = [1,5,4,3,2]
return = 2
Swap 5 with 2, then swap 4 with 3.

Constraints
1 ≤ values.length ≤ 2 × 10^5
-10^9 ≤ values[i] ≤ 10^9
All values are distinct.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: sort a copy of the array and map each value to its target index. Now the array is a permutation of positions. Each position points to where its value belongs, and those pointers form cycles. A cycle of length k needs exactly k-1 swaps, so the answer is the sum of (length - 1) over all cycles, or n minus the number of cycles. Check example 1: [4,3,1,2] maps to targets [3,2,0,1]. Position 0 goes to 3, 3 goes to 1, 1 goes to 2, 2 goes to 0. That's one cycle of length 4, so 3 swaps. Matches. The common pitfall is simulating swaps one at a time, which goes quadratic with n up to 2 x 10^5. Another is forgetting a visited array and double counting cycles. Values are distinct, so a hash map or sorted index pairs both work. Total cost is O(n log n). If the cycle idea doesn't come to you under the clock, StealthCoder is the hedge.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Minimum Swaps 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. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Postman's OA.

Postman reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Swaps to Sort an Array FAQ

What's the trick to Minimum Swaps to Sort an Array?+

Treat the array as a permutation. Sort to find each value's target index, then count cycles in the mapping. Each cycle of length k costs k-1 swaps. The answer is n minus the number of cycles. No swap simulation needed.

How hard is this problem really?+

Medium. The code is short, but the cycle insight is what trips people. If you've seen permutation cycles before, it's quick. If not, brute-force swapping feels natural and ends up too slow for 2 x 10^5 elements.

What time complexity should I aim for?+

O(n log n), dominated by the sort. The cycle walk afterward is O(n) with a visited array. Anything quadratic will likely time out given the upper constraint of 2 x 10^5 elements.

How do I handle the value to index mapping?+

Pair each value with its original index, sort the pairs by value, then the sorted position is the target. Since all values are distinct, a hash map from value to sorted index works too. Pairs avoid hashing overhead.

How do I prepare for this in 48 hours?+

Write the cycle-counting solution from scratch twice. Trace example 1 by hand until the single 4-cycle is obvious. Then test edge cases: already sorted, length 1, and a fully reversed array. That covers most of what the OA will probe.

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

OA at Postman?
Invisible during screen share
Get it