Fewest Cross-Replica Swaps to Make Two Flag Strings Identical
Last updated: November 29, 2026
Quick Overview
Two replicas store the same feature-flag vector as strings of 'x' (on) and 'y' (off), but they drifted. One operation swaps a character of the first string with any character of the second; return the minimum swaps to make them identical, or -1 if impossible.
0
0
0 solved
Two replicas store the same feature-flag vector as strings of 'x' (on) and 'y' (off), but they drifted. One operation swaps a character of the first string with any character of the second; return the minimum swaps to make them identical, or -1 if impossible.
A counting greedy that Google interviewers like because the right answer comes from classifying mismatches, not from simulating swaps. They check that you pair same-type mismatches first and spot the parity condition.
What the Interviewer Expects
- Ignore positions that already match
- Count the two mismatch types (x over y, y over x)
- Fix two same-type mismatches with one swap
- Fix a leftover pair of different types with two swaps
- Detect the odd-total impossible case
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Swaps are only allowed at the same index.
- Strings use three symbols.
- Return the actual swap operations.
- Swaps within the same string are also allowed.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: strings s1, s2 of equal length over {x, y}. One move swaps s1[i] with s2[j] for any i, j. Output: minimum moves to make s1 == s2, or -1....
High-Level Design
Only mismatched positions matter. Let xy count positions with s1 = x, s2 = y, and yx the reverse. Two xy positions i and j are fixed by one swap: swap s1[i] with s2[j]; then position i r...