Reported September 2026
Amazongreedy

Minimum Adjacent Swaps to Group Binary Values

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

The data structure behind this Amazon OA question from September 2026 is barely a data structure at all. It's a running count, and the hint says hash-table, but don't go hunting for a map. You get a binary array, adjacent swaps only, and you need the cheaper of two target layouts: all 0s first or all 1s first. With 100000 elements, anything quadratic dies. If you've got an invite in your inbox, the whole problem is counting inversions in one pass, twice. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment.

The problem

You are given a binary array bits. Using adjacent swaps, rearrange it so that equal values form two contiguous groups.
Either order is valid: all 0s before all 1s, or all 1s before all 0s. Return the minimum number of adjacent swaps over both orders.

Function
minimumAdjacentSwaps(bits: int[]) → long

Examples
Example 1
bits = [0,1,0,1]
return = 1
Swap the middle 1 and 0 to obtain [0,0,1,1].
Example 2
bits = [1,1,0,0]
return = 0
The array already has all ones before all zeroes.

Constraints
1 <= bits.length <= 100000
Every value in bits is either 0 or 1.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: the minimum adjacent swaps to push all 0s left equals the number of (1, 0) pairs where the 1 comes before the 0. Scan once, keep a count of 1s seen so far, and every time you hit a 0, add that count to the total. That's the cost for zeros-first. For ones-first, count (0, 1) pairs the same way, with a count of 0s seen. Return the smaller total. The common pitfall is overflow. With 100000 elements the pair count reaches about 2.5 billion, so use a 64-bit long, which the signature already hints at. Another trap is simulating swaps or sorting, which is too slow. It's O(n) time and O(1) space. If the logic slips away during the live OA, StealthCoder can hand you the two-pass counting solution while you keep your composure.

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 Minimum Adjacent Swaps to Group Binary Values 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

⏵ 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.

Minimum Adjacent Swaps to Group Binary Values FAQ

What's the actual trick in this Amazon OA problem?+

Each adjacent swap fixes exactly one inverted pair. So the answer for zeros-first is the count of pairs where a 1 appears before a 0. Compute it in one scan by tracking how many 1s you've seen. Do the mirror count for ones-first and take the minimum.

Do I need a hash table even though the pattern hint says so?+

No. A hash table isn't needed here. You only need two integer counters per direction. The hint is loose. The real technique is counting inversions with a running tally, which keeps the solution at O(n) time and constant space.

Why does the return type use long?+

The worst case is an array like half 1s followed by half 0s. That's about 50000 times 50000, roughly 2.5 billion swaps, which overflows a 32-bit int. Accumulate in a 64-bit integer from the start so you don't get silent wraparound on large tests.

How do I check my answer on the examples?+

For [0,1,0,1], zeros-first has one inverted pair (the middle 1 before the last 0), so the cost is 1. Ones-first costs more, so the minimum is 1. For [1,1,0,0], zeros-first costs 4 but ones-first costs 0, so you return 0.

How do I prepare for this in 48 hours?+

Practice the inversion-counting idea on a few binary arrays by hand, then code the single-pass version both directions. Test edge cases: length 1, all same values, already sorted. Spend the rest of the time on the overflow check and clean loop logic, not on fancy structures.

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