Reported April 2026
Rubriktwo pointers

Minimum Swaps To Binary Palindrome

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

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

The mistake that sinks most first attempts at Minimum Swaps To Binary Palindrome is counting mismatched pairs and returning that number. Rubrik reported this one in April 2026, and the example shows why it fails. "101000" has two mismatched pairs but the answer is 1. The swaps can be between any two characters, not just neighbors, and that changes the whole count. It's a two-pointer scan with one line of math at the end. If you blank on the OA, StealthCoder runs invisibly on your desktop and surfaces the approach in real time. Read the trick below first, though. It takes five minutes to learn and you'll recognize it on sight.

The problem

Given a binary string s consisting only of '0' and '1', return the minimum number of swaps required to rearrange s into a palindrome.
In one swap, you may swap any two characters in the string; the swapped characters do not need to be adjacent. If it is impossible to make s a palindrome, return -1.

Function
minSwapsToPalindrome(s: String) → int

Examples
Example 1
s = "101000"
return = 1
Swap the '1' at index 2 with the '0' at index 5 to obtain "100001", which is a palindrome.

Constraints
The source post did not specify numeric constraints.

Reported by candidates. Source: FastPrep

Pattern and pitfall

First, feasibility. Count the zeros and ones. For even length, both counts must be even. For odd length, exactly one count may be odd. Otherwise return -1. Next, run two pointers from both ends and count mismatched pairs, call it m. Each swap can fix two mismatched pairs at once. Take a (0,1) pair and a (1,0) pair, swap the 1 from one with the 0 from the other, and you get (0,0) and (1,1). So the answer is ceil(m/2). If m is odd, the leftover pair gets fixed by swapping with the middle character, which only exists when the length is odd, and the feasibility check already guarantees that works. The pitfall is reaching for the adjacent-swap greedy from the classic string palindrome problem, or returning m. Both overcount. Skip the simulation, it's O(n) and O(1) space. If the formula slips away mid-assessment, StealthCoder is the safety net that hands it back.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Minimum Swaps To Binary Palindrome 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Rubrik reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Swaps To Binary Palindrome FAQ

What's the trick in Minimum Swaps To Binary Palindrome?+

Swaps are between any two positions, so one swap fixes two mismatched mirror pairs. Count pairs where s[i] != s[n-1-i], call it m, and return ceil(m/2). Check feasibility first by looking at the parity of the zero and one counts. No simulation needed.

How do I know when to return -1?+

Count zeros and ones. A palindrome allows at most one character with an odd count, and only when the length is odd. For even length, both counts must be even. If that fails, return -1 before doing any pair counting.

Why is the answer 1 for "101000" and not 2?+

Mirror pairs are (1,0), (0,0), (1,0). Two are mismatched. Swap the 1 at index 2 with the 0 at index 5 and both mismatches resolve at once, giving "100001". That's why you divide the mismatch count by two, rounded up.

What happens when the mismatch count is odd?+

One mismatched pair is left after pairing the rest. That only occurs for odd-length strings, where the middle character can be swapped with one side of the leftover pair to make it match. It costs one swap, which is exactly what rounding up gives you.

How should I prepare in 48 hours for this Rubrik OA?+

Write this one from memory twice: the parity check, the two-pointer mismatch count, then ceil(m/2). Then test on small strings by hand, including odd length and all-same characters. Also know why adjacent-swap solutions don't apply here, since that confusion is the common failure.

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

OA at Rubrik?
Invisible during screen share
Get it