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 Name | Type |
|---|---|
| id | int |
| student | varchar |
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
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
โ Linear Growth
Space Complexity
O(1)
No additional space needed beyond the result set
โ 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code