Seat Each Arriving Engineer as Far as Possible From Others

Last updated: November 29, 2026

Quick Overview

A quiet focus room has n desks in a row numbered 0 to n-1. When an engineer arrives, seat() assigns the desk that maximizes the distance to the nearest occupied desk (lowest number on ties, desk 0 if the room is empty); leave(p) frees desk p.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


A quiet focus room has n desks in a row numbered 0 to n-1. When an engineer arrives, seat() assigns the desk that maximizes the distance to the nearest occupied desk (lowest number on ties, desk 0 if the room is empty); leave(p) frees desk p.

A Google technical screen simulation of an occupancy policy, in the spirit of office desk booking tools. The interviewer checks the three candidate gap types (left edge, interior gaps, right edge), correct tie-breaking, and a sensible data structure choice for n up to 10^9.

What the Interviewer Expects
  • Store only occupied desks in sorted order, since n can be huge
  • Consider the left edge, each interior gap midpoint, and the right edge
  • Break ties toward the lowest desk number
  • Implement leave with binary search removal
  • Discuss O(k) per seat and how a heap of gaps reaches O(log k)
Key Topics to Cover
simulation
sorted list
binary search
greedy
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Implement the heap-of-gaps version with lazy invalidation for O(log k) seats.
  • What if desks are arranged in a circle?
  • How would you extend this to a 2D grid of desks?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

n desks, up to 10^9, and up to 10^4 calls. seat() returns the chosen desk and marks it occupied. leave(p) is only called for occupied desks. Distance between desks i and j is |i - j|....

High-Level Design

Keep taken, a sorted Python list. For seat(): if empty, choose 0. Otherwise evaluate candidates in increasing desk order so the first strictly better candidate wins ties toward lower numbers: desk...


Submit Your Answer
Markdown supported