Exchange Neighboring Seat Assignments in the Result

Quick Overview

Swap neighboring seat assignments in pairs and leave the final assignment alone when the count is odd.

Exchange Neighboring Seat Assignments in the Result

Company: Tesla

Role: Software Engineer

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

### Problem Return the Seat assignment after exchanging each consecutive pair of students: seats 1 and 2, then 3 and 4, and so on. If the student count is odd, the final student stays in that seat. Sort the returned table by id ascending. Return the resulting assignments as a query. ### Tables Table: `Seat` ```text +-------------+---------+ | 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. ``` ### Example ```text Seat (id,student): (1,Ada), (2,Bo), (3,Cam), (4,Dee), (5,Eli) Output (id,student): (1,Bo), (2,Ada), (3,Dee), (4,Cam), (5,Eli) ``` ### What a Strong Answer Covers - Swap students in each consecutive pair of seats. - Preserve the final student when the count is odd and sort by seat ID. ### Follow-up Questions - How would the answer change if seat IDs had gaps? - How would you rotate groups of three seats instead of swapping pairs?

Overview: Swap neighboring seat assignments in pairs and leave the final assignment alone when the count is odd.

Tesla logo
Tesla
mediumSoftware EngineerOtherData Manipulation (SQL/Python)
0
0

Problem

Return the Seat assignment after exchanging each consecutive pair of students: seats 1 and 2, then 3 and 4, and so on. If the student count is odd, the final student stays in that seat. Sort the returned table by id ascending. Return the resulting assignments as a query.

Tables

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.

Example

Seat (id,student): (1,Ada), (2,Bo), (3,Cam), (4,Dee), (5,Eli)
Output (id,student): (1,Bo), (2,Ada), (3,Dee), (4,Cam), (5,Eli)

What a Strong Answer Covers Guidance

  • Swap students in each consecutive pair of seats.
  • Preserve the final student when the count is odd and sort by seat ID.

Follow-up Questions Guidance

  • How would the answer change if seat IDs had gaps?
  • How would you rotate groups of three seats instead of swapping pairs?