Table: Seat
+-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | student | varchar | +-------------+---------+ id is the primary key (unique value) column for this table. Each row of this table indicates the name and the ID of a student. The ID sequence always starts from 1 and increments continuously.
Write a solution to swap the seat id of every two consecutive students. If the number of students is odd, the id of the last student is not swapped.
Return the result table ordered by id in ascending order.
The result format is in the following example.
Example 1:
Input: Seat table: +----+---------+ | id | student | +----+---------+ | 1 | Abbot | | 2 | Doris | | 3 | Emerson | | 4 | Green | | 5 | Jeames | +----+---------+ Output: +----+---------+ | id | student | +----+---------+ | 1 | Doris | | 2 | Abbot | | 3 | Green | | 4 | Emerson | | 5 | Jeames | +----+---------+ Explanation: Note that if the number of students is odd, there is no need to change the last one's seat.
In #626 Exchange Seats, the goal is to swap every pair of adjacent seat IDs in a student table. For example, seat 1 should swap with seat 2, seat 3 with seat 4, and so on. If the total number of seats is odd, the final student keeps their original seat.
A common approach is to use a SQL conditional expression such as CASE to determine the new seat ID based on whether the current seat number is odd or even. Odd seat IDs typically shift forward, while even seat IDs shift backward. However, you must also check whether the seat is the last one when the total count is odd so it remains unchanged.
Another way to think about the problem is by using window functions or calculating the maximum seat ID to detect edge cases. After computing the new seat positions, the result is ordered by the updated seat number to produce the final arrangement. Since the query processes each row once, the overall complexity is linear.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| CASE-based seat swapping | O(n) | O(1) |
| Window function / conditional logic | O(n) | O(1) |
Frederik Müller
This approach involves iterating through the list and swapping every two consecutive students. The loop runs with a step of 2 to handle even-indexed pairs, ensuring that swaps are done pair-wise. If the total number of students is odd, the last student remains in their original position as there is no pair to swap with.
Time Complexity: O(n), since each element may be swapped once.
Space Complexity: O(1), as no extra space is used except the temporary variable for swapping.
1def swap_seats(students):
2 for i in range(0, len(students) - 1, 2):
3 students[i], students[
In Python, lists (which are mutable) are utilized to store students’ names. The swap occurs using tuple unpacking, a simple way to efficiently exchange the two elements at adjacent positions.
This method employs a temporary array or list to store swapped results before copying them back to the original list or directly returning them as new results. This makes the logic conceptually simpler at the cost of some additional space usage.
Time Complexity: O(n) due to linear traversal.
Space Complexity: O(n), using a full-sized temporary array.
1
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, variations of this problem appear in SQL interviews because it tests conditional logic, ordering, and edge case handling. It is commonly used to evaluate practical query-writing skills.
The optimal approach uses SQL conditional logic such as a CASE expression to swap adjacent seat IDs. Odd IDs are shifted forward and even IDs backward, while ensuring the last seat remains unchanged if the total count is odd.
The problem mainly relies on conditional expressions like CASE along with ordering logic. Some solutions also use aggregate functions or window functions to detect the last seat.
If the number of seats is odd, the final seat does not have a pair to swap with. In that case, the query keeps that seat ID unchanged while swapping all other adjacent pairs.
By using array spread syntax, JavaScript efficiently duplicates the original array before modifying it. This supports the principle of keeping original records intact by conducting operations on a separate data structure.