Exam Room

Medium · Heap & Priority Queue · about 30 minutes

Asked at Google, Uber, Amazon, Apple, Salesforce.

An exam room has n seats numbered 0..n-1.

  • ExamRoom(n) creates the room.
  • seat() seats a student where the distance to the closest other student is maximized (the lowest-numbered

seat on a tie; seat 0 if the room is empty) and returns the seat number.

  • leave(p) frees seat p (it's occupied).

Related guides