Assign dorm rooms by student preference
I was asked this
No reports yet. Sign in to say where you were asked it.
An object-oriented design interview question asked in Google's onsite coding round, last asked Feb 2026. It tests object-oriented design and allocation.
Design the object model for a dormitory room-assignment system. You get a set of students and a set of rooms. Each room is either a two-person room or a four-person room, with that many beds. Each student states one preference: a two-person or a four-person room. Assign students to rooms so that preferences are respected and no room is over-filled. The algorithm is intentionally simple; the focus is class design, data structures and the assignment logic, including what happens when a preference cannot be met.
Requirements
- Classes:
Student(id, preference),Room(id, room type, beds, current occupants), aRoomTypewith its capacity, and a service that owns the assignment run and its result. - Operations: load students and rooms; run the assignment; look up a student's room; list a room's occupants; report unassigned students and why.
- Rules: a student is in at most one room; a room never holds more students than its beds; the run is a batch over in-memory data (hundreds to low thousands of students) and can be re-run.
Clarifying questions
- Is a preference a hard requirement, or may a student fall back to the other room type when theirs is full?
- May rooms be partly filled (three students in a four-person room)?
- Are there grouping rules: friends together, floors by gender, accessibility needs?
- What should happen to students who cannot be placed: an unassigned list, an error, any free bed?
- Do students and rooms arrive all at once, or over a registration period?
- Are ids unique? Can a student appear twice?
What a strong answer covers
- Room types as an enum or capacity field rather than a subclass per size, so adding a six-person room is a one-line change.
- Available rooms grouped by type, for example
dict[RoomType, deque[Room]], with each room tracking free beds; a room leaves the pool when full. Assignment pops from the requested type's pool. - A clear result object: assignments, unassigned students with a reason, and capacity summaries.
- The fallback policy as a pluggable strategy (strict, fall back to the other type, fill the fewest rooms), not hard-coded
ifs. - Determinism (stable ordering of students and rooms) so the same input always gives the same output, and unit tests that prove capacity is never exceeded and every student is accounted for.
- Validation of input: duplicate ids, unknown preferences, rooms with zero beds.
Follow-ups
- Students submit a ranked list of preferences instead of one.
- Students register one at a time over two weeks: support online assignment and changes.
- Groups of friends must share a room when possible. What new conflicts appear?
- How would you make the assignment fair when demand for four-person rooms exceeds supply?
Where it's asked
- Google · Onsite coding · Last asked Feb 202645 min · Usually two rounds, mostly in person: whiteboard plus a Chromebook with Google Docs · No AI (a pilot code-comprehension round lets candidates use Gemini)
What it tests
object-oriented design · allocation
No official answer yet. Draw yours on the right; publish it and it shows under Community.
Nothing published yet. Publish your canvas and it shows here for others to open and remix.
