Minimum Swaps to Group All 1's Together
A medium-tier problem at 61% community acceptance, tagged with Array, Sliding Window. Reported in interviews at Expedia and 0 others.
You've got an array of 0s and 1s, and you need to group all the 1s together with the minimum number of swaps. Expedia asks this one, and it looks simpler than it is. The trap is thinking you need to try every possible position and count swaps each time, which tanks your runtime. The real solution is sliding window. Once you see it, it's clean. If this hits your live assessment and you blank on the pattern, StealthCoder surfaces a working solution invisible to the proctor.
Companies that ask "Minimum Swaps to Group All 1's Together"
Minimum Swaps to Group All 1's Together is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by a senior engineer who knows the OA is theater. This is the script.
Get StealthCoderThe key insight: the final block of 1s must fit somewhere in the array, and that somewhere is a fixed-size window. Count how many 1s exist total. Slide a window of that size across the array and count zeros in each window. The window with the fewest zeros is where your 1s should go, and the swap count equals the number of zeros in that window. Most candidates try brute force or recursive approaches, which fail the time limit. Sliding Window is the topic here, but the actual trick is recognizing that you don't care about final positions, only about the window with the best ratio of existing 1s. If you're stuck during the assessment, StealthCoder runs invisibly and shows you a solution in seconds.
Pattern tags
You know the problem.
Make sure you actually pass it.
Minimum Swaps to Group All 1's Together recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by a senior engineer who knows the OA is theater. This is the script. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Swaps to Group All 1's Together interview FAQ
Is this really a medium, or is it harder than it looks?+
It sits at 61% acceptance, which means it's genuinely medium but not trivial. The difficulty isn't complexity, it's pattern recognition. Once you see sliding window, it's straightforward. Most failures come from overthinking the swap mechanics instead of recognizing the window trick.
What's the most common wrong approach?+
Brute force: iterate every possible target position, count swaps for each, return the minimum. This works but is O(n squared) and times out. The sliding window solution is O(n) and runs instantly. That gap is why people miss it under pressure.
How does sliding window apply here specifically?+
Window size equals total count of 1s. Slide it once across the array, count zeros in each window. Minimum zeros found is your answer. You're not swapping in the solution itself, just measuring the cost of each possible window. That decoupling is the trick.
Will Expedia ask variations of this, or the exact version?+
Usually exact. Expedia's version is stable, but variations exist (swaps with adjacent elements, different constraints). Know the base pattern cold so you can adapt. The sliding window core transfers to variants.
How should I practice this before my OA?+
Solve it twice: once with sliding window, once with brute force. Compare runtimes. Then solve without looking at notes. The window-counting insight should feel automatic before the real assessment. That's when you're ready.
Want the actual problem statement? View "Minimum Swaps to Group All 1's Together" on LeetCode →