Reported September 2026
Akuna Capitalhash table

Minimum Swaps

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

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

The hash map is the whole game in this Akuna Capital OA, reported in September 2026. Minimum Swaps hands you an array of unique popularity ratings and asks for the fewest swaps to sort it in decreasing order. It looks like a sorting question, but you're really counting cycles in a permutation. If the cycle idea doesn't click under the clock, StealthCoder runs invisibly on your screen during the live assessment and gives you a working solution as a safety net. Know the trick first, though. It's about ten lines.

The problem

You are given an array popularity containing the unique popularity ratings of n items.
The shopkeeper wants the items arranged from left to right in decreasing popularity.
In one operation, the shopkeeper can swap any two items.
Your task is to determine the minimum number of swaps needed to achieve the correct decreasing order.

Function
minimumSwaps(popularity: int[]) → int

Examples
Example 1
popularity = [3, 4, 1, 2]
return = 2
Suppose there are n = 4 items, and popularity = [3, 4, 1, 2].
Output: 2
Explanation
First swap: Switch items with ratings 3 and 4 to get [4, 3, 1, 2].
Second swap: Switch items with ratings 1 and 2 to get [4, 3, 2, 1].

Constraints
1 ≤ n ≤ 2 × 10^5
1 ≤ popularity[i] ≤ n
Test Case Input FormatThe first line contains the integer n.
The next n lines contain an integer element of popularity[i].

Reported by candidates. Source: FastPrep

Pattern and pitfall

Sort a copy of the array in decreasing order and build a hash map from each value to its target index. Since ratings are unique, every element has exactly one destination. Now treat index i pointing to target[popularity[i]] as a permutation graph. Each cycle of length k needs k-1 swaps. So walk the array with a visited array, follow each unvisited cycle, and add length minus 1 to the answer. Total is O(n log n) for the sort and O(n) for the cycle walk, which fits n up to 2 x 10^5. The common pitfall is simulating swaps with a nested loop, which times out at that size. Another is sorting ascending by habit when the target is decreasing. Check Example 1: [3,4,1,2] has two 2-cycles, so the answer is 2. If you blank on the cycle logic mid-OA, StealthCoder is the hedge that keeps you moving.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Minimum Swaps 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 by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Akuna Capital reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Swaps FAQ

What's the trick in Minimum Swaps?+

Think of the array as a permutation and count cycles. Sort a copy in decreasing order, map each value to its target index, then follow each cycle. A cycle of length k costs k-1 swaps. Sum those and you're done.

How hard is this Akuna Capital question really?+

Medium. The code is short, but you need to see the cycle structure. Once you know it, it's easy. Without it, people try greedy swapping or brute force and run out of time on large inputs.

Does the decreasing order change anything versus ascending?+

Only the target positions. Sort descending to get each value's destination index. The cycle counting logic is identical. Mixing up the direction is the classic silent bug, so test it on [3,4,1,2].

What time complexity should I aim for?+

O(n log n) from sorting, plus O(n) for the cycle walk and hash map. With n up to 2 x 10^5, anything quadratic will fail. Don't simulate swaps one by one with repeated scans for the max element.

How do I prepare for this in 48 hours?+

Write the cycle-count solution from scratch twice. Test on [3,4,1,2], an already sorted array, and a single element. Then try a reversed array. Those cases cover the edge behavior and lock in the pattern before the OA.

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

OA at Akuna Capital?
Invisible during screen share
Get it