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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

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
Union-Find
Permutation Cycles
Graph Modeling
Greedy
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: row, a permutation of 0..2k-1.
  • Partners are (2i, 2i+1), so person p belongs to couple p // 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...


Submit Your Answer
Markdown supported