Exchange Seats - Problem

๐Ÿช‘ Exchange Seats

You're managing a classroom where students need to swap seats in pairs! Given a table Seat with student information, you need to swap the seat IDs of every two consecutive students.

Table Schema:

Column NameType
idint
studentvarchar

Rules:

  • Student 1 swaps with Student 2
  • Student 3 swaps with Student 4
  • And so on...
  • If there's an odd number of students, the last student stays in their original seat

Return the result ordered by id in ascending order. This problem tests your SQL skills with conditional logic and row manipulation.

Input & Output

example_1.sql โ€” Basic Even Number of Students
$ Input: Input: Seat table: +----+---------+ | id | student | +----+---------+ | 1 | Abbot | | 2 | Doris | | 3 | Emerson | | 4 | Green | +----+---------+
โ€บ Output: Output: +----+---------+ | id | student | +----+---------+ | 1 | Doris | | 2 | Abbot | | 3 | Green | | 4 | Emerson | +----+---------+
๐Ÿ’ก Note: With 4 students (even), all pairs swap: (1โ†”2) and (3โ†”4). Abbot moves from seat 1 to 2, Doris moves from seat 2 to 1, Emerson moves from seat 3 to 4, Green moves from seat 4 to 3.
example_2.sql โ€” Odd Number of Students
$ Input: Input: Seat table: +----+---------+ | id | student | +----+---------+ | 1 | Abbot | | 2 | Doris | | 3 | Emerson | | 4 | Green | | 5 | Jeames | +----+---------+
โ€บ Output: Output: +----+---------+ | id | student | +----+---------+ | 1 | Doris | | 2 | Abbot | | 3 | Green | | 4 | Emerson | | 5 | Jeames | +----+---------+
๐Ÿ’ก Note: With 5 students (odd), pairs (1โ†”2) and (3โ†”4) swap, but student 5 (Jeames) has no pair and stays in the same seat.
example_3.sql โ€” Single Student Edge Case
$ Input: Input: Seat table: +----+---------+ | id | student | +----+---------+ | 1 | Alice | +----+---------+
โ€บ Output: Output: +----+---------+ | id | student | +----+---------+ | 1 | Alice | +----+---------+
๐Ÿ’ก Note: With only 1 student, there's no pair to swap with, so Alice remains in seat 1.

Visualization

Tap to expand
Seat Exchange ProcessBefore:1:Alice2:Bob3:Carol4:Dave5:EveSTAYSAfter:1:Bob2:Alice3:Dave4:Carol5:EveOdd Position StudentsEven Position StudentsUnpaired (Stays)
Understanding the Visualization
1
Initial Seating
Students are seated in sequential order by ID
2
Identify Pairs
Group students into pairs: (1,2), (3,4), etc.
3
Swap Within Pairs
Each pair exchanges seats with their partner
4
Handle Odd Case
If odd total, last student stays in original seat
Key Takeaway
๐ŸŽฏ Key Insight: Use modulo arithmetic to determine seat swapping: `id % 2 = 1` means odd (move right), `id % 2 = 0` means even (move left), with special handling for the unpaired last seat.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the table to apply the CASE logic

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

No additional space needed beyond the result set

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค Number of students โ‰ค 1000
  • id is a continuous sequence starting from 1
  • student names are unique varchar strings
  • The table is guaranteed to have sequential IDs with no gaps
Asked in
Amazon 25 Microsoft 18 Google 12 Meta 8
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen