Minimum Adjacent Transpositions
Reported by candidates from Epic's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Epic reported this one in September 2026, and the detail that matters is in the constraints: strings up to 200000 characters with repeated ASCII letters. That rules out any simulation of swaps. "GUM" to "MUG" costs 3 and "aabb" to "bbaa" costs 4, and both numbers are really inversion counts in disguise. If you have this OA coming in a day or two, the job is to turn the strings into a permutation and count inversions fast. StealthCoder sits invisibly on your screen as a safety net if you blank on the mapping step during the live OA.
The problem
source and target contain the same ASCII characters with the same frequencies. In one operation, swap two adjacent characters in source. Return the minimum number of operations required to transform source into target. Function minimumAdjacentSwaps(source: String, target: String) → long Examples Example 1 source = "GUM" target = "MUG" return = 3 Moving M to the front takes two swaps, then moving U ahead of G takes one more. Example 2 source = "aabb" target = "bbaa" return = 4 Each of the two b characters crosses both a characters. Constraints 1 <= source.length == target.length <= 200000 Both strings contain the same ASCII characters with the same frequencies.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to map each character in source to its target position, then count inversions in that index sequence. For duplicates, match the k-th occurrence of a character in source to the k-th occurrence in target. That's the greedy assignment that never crosses equal characters, so it's optimal. Build a queue of target indices per character, walk source left to right, and pop the next index for each character. You now have a permutation array. The answer is the number of inversions, computed in O(n log n) with a Fenwick tree or merge sort. Pitfalls: using O(n^2) bubble-style counting, which dies at 200000, and overflowing int. The return type is long for a reason, since n(n-1)/2 is huge. Also don't swap equal characters, because it wastes operations. If the mapping or the BIT indexing slips under pressure, StealthCoder is the hedge that hands you a working solution during the live OA.
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 Minimum Adjacent Transpositions 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 Epic's OA.
Epic 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 Adjacent Transpositions FAQ
What's the trick in Minimum Adjacent Transpositions?+
Convert the problem into counting inversions. Map each source character to a target index, pairing the k-th occurrence of each character in source with the k-th in target. The minimum adjacent swaps equals the inversion count of that index array.
How do I handle duplicate characters?+
Keep a list of target positions per character, in order. As you scan source, assign each character the next unused position from its list. Pairing in order guarantees equal characters never cross, which is what keeps the swap count minimal.
Why can't I just simulate the swaps?+
With length up to 200000, simulation or bubble-sort counting is O(n^2) and times out. You need O(n log n) inversion counting using a Fenwick tree or a merge sort that counts cross-half pairs while merging.
Why does the function return a long?+
The worst case is about n(n-1)/2 inversions. For n = 200000 that's roughly 2 x 10^10, far beyond a 32-bit int. Use a 64-bit accumulator for the count, or you'll get wrong answers on large tests.
How should I prepare for this in 48 hours?+
Write inversion counting twice, once with a Fenwick tree and once with merge sort. Then practice the duplicate-mapping step with per-character queues. Test with the two examples: GUM to MUG gives 3, aabb to bbaa gives 4.