Drill 4 of 12 in General questions
0

Interview Question Bank · CASE · modulo · last odd row

4. Swap neighbouring seats

MediumGeneralQuestion #196

Concept: CASE · modulo · last odd rowshow

To swap each pair of neighbours, rewrite the id instead of moving the names: odd ids become id + 1 and even ids become id - 1, which seat_id % 2 tells apart. With an odd number of rows the last odd id has no partner, so it needs its own branch that compares it with MAX(id). Then sort by the new id.

Example (different data):

SELECT CASE WHEN slot % 2 = 1 THEN slot + 1 ELSE slot - 1 END AS new_slot, speaker
FROM talk_slots
ORDER BY new_slot;

Your task

Fernbrook Academy reshuffles its exam hall so nobody sits next to the same person twice: students in seats 1 and 2 swap, 3 and 4 swap, and so on. If the last seat has no partner, that student stays put.

Return seat_id (the new seat) and student for everyone after the swap. Sort by the new seat_id.

Starting Postgres in your browser…

Checking your access…