Min Swaps
Reported by candidates from JP Morgan's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
JP Morgan's Min Swaps question hit the circuit in September 2024, and it's a classic sorting + greedy problem that looks deceptively simple. You've got an array, you need to minimize swaps to reach some target state, and the trap is overthinking the order in which you perform operations. The pattern is old but the execution matters. This is exactly the kind of problem where blanking on the approach for 30 seconds can cost you, which is why having a safety net like StealthCoder running during the live OA pays off.
Pattern and pitfall
Min Swaps typically boils down to: map each element to its target position, then count the minimum swaps needed to sort or rearrange the array into that state. The greedy trick is to process cycles. If element A belongs in position B, and element B belongs in position C, you've found a cycle. The number of swaps to resolve a cycle of length N is N minus 1. The pitfall is trying to simulate swaps one by one instead of identifying cycles upfront. Most candidates either brute force (slow) or miss the cycle structure entirely. The algorithm: hash map of values to target indices, iterate through positions, detect cycles, sum up (cycle length minus 1) for each cycle. Time complexity is O(n). When you're live and the problem text is on screen, StealthCoder reads the exact constraints and gives you the cycle-based solution instantly, bypassing the 5 minutes of mental thrashing.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Min 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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass JP Morgan's OA.
JP Morgan 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.
Min Swaps FAQ
Is this the 'Min Swaps to Sort' LeetCode 1059 problem or something else?+
Possibly, but JP Morgan's version may have a twist. The core is cycle detection in permutations. If you can't see the problem text live, assume cycles. That's the pattern 99% of the time on this title.
Do I need to actually perform the swaps, or just count them?+
You count them. The answer is the sum of (cycle_length - 1) for all cycles in the permutation. Simulating swaps is slower and error-prone. Think math, not simulation.
What if the array has duplicates?+
Duplicates change the problem. If the problem allows duplicates, you may need to match indices greedily, not by cycle. Read the constraints carefully. StealthCoder will catch this nuance from the live problem statement.
How much time should I spend on this in the OA?+
15-20 minutes if you know the cycle trick, 40+ if you don't. JP Morgan OAs are typically 90 minutes for 2-3 problems, so this is medium-priority. Don't waste 30 minutes guessing.
Is there a way to prep this in 24 hours?+
Yes. Solve LeetCode 1059 and understand cycle detection in permutations. The concept transfers 1-to-1. Ignore brute force solutions. Focus on the hash map and cycle identification approach.