Minimum Swaps to Seat Partner Pairs Side by Side in a Row
Last updated: November 29, 2026
Quick Overview
People labeled 0 to 2k-1 sit in a row of 2k seats, where persons 2i and 2i+1 are partners. A swap exchanges any two people. Return the minimum number of swaps so every partner pair occupies seats (0,1), (2,3), and so on.
0
0
0 solved
People labeled 0 to 2k-1 sit in a row of 2k seats, where persons 2i and 2i+1 are partners. A swap exchanges any two people. Return the minimum number of swaps so every partner pair occupies seats (0,1), (2,3), and so on.
A Google onsite question about permutation cycles in disguise, here framed as seating for a team offsite. The interviewer checks whether you model seat pairs as edges between couples and know that a cycle of k couples needs k - 1 swaps, which union-find counts as the number of successful merges.
What the Interviewer Expects
- Models each seat pair as an edge between two couple IDs
- Recognizes the resulting graph is a union of cycles
- States that a cycle of k couples needs exactly k - 1 swaps
- Counts swaps as successful unions
- Compares with a greedy swap-in-place approach
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
- Prove the greedy approach of fixing seats left to right is optimal.
- What if partners form groups of three in triples of seats?
- Return the actual swaps performed.
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:
row, a permutation of0..2k-1. - Partners are
(2i, 2i+1), so person p belongs to couplep // 2. - Output: minimum number of swaps....
High-Level Design
Make couple IDs the nodes. Each seat pair (row[2s], row[2s+1]) gives an edge between the two couples sitting there. Every couple has exactly two members, so every node has degree two and the graph i...