KatInterview

Bunk Allocation by Preferred Room Size

Data Structure DesignAuto-graded · 20 test cases
EasyAuto-gradedData Structure Designqueuessimulationhash-map

Last updated

Match students to rooms by hard size preference in input order, respecting capacity, keeping partially filled rooms, and reporting students who could not be placed.

Bunk Allocation by Preferred Room Size

A residence hall needs to place students into rooms. Every room has a capacity of either 2 or 4 beds. Every student asks for exactly one room size, either 2 or 4. A preference is a hard requirement: a student may only be placed in a room whose capacity equals their preference.

Write a function that performs a single batch allocation and returns the result.

Input

  • students: a list of dictionaries, each with
    • "id": a unique string identifying the student
    • "preference": the integer 2 or 4
  • rooms: a list of dictionaries, each with
    • "id": a unique string identifying the room
    • "capacity": the integer 2 or 4

Allocation rules

  1. Process rooms in the order they appear in rooms.
  2. Students who want a given room size form a waiting line, ordered by their position in students.
  3. For each room, take students from the front of the waiting line for that room's capacity, one at a time, until the room has capacity occupants or the line is empty.
  4. Rooms may end up partially filled or empty. Every room still appears in the output.
  5. A student is placed in at most one room and is never placed in a room of a different size than they asked for.
  6. Students who are not placed in any room are reported as unassigned.

Output

Return a dictionary with two keys:

  • "assignments": a list with one entry per room, in the same order as rooms. Each entry is a dictionary {"room_id": <room id>, "student_ids": <list of student ids in the room>}. The ids inside student_ids appear in the order they were placed, which is the input order of the students.
  • "unassigned": a list of ids of students not placed in any room, in the same order as they appear in students.

Constraints

  • Student ids are unique; room ids are unique.
  • Each preference and each capacity is either 2 or 4.
  • students and rooms may each be empty.
  • Input sizes are at most a few thousand entries.
  • Demand may exceed or fall short of supply; this must never overfill a room or raise an error.

Examples

Example 1

Input: allocate_rooms([{'id': 's1', 'preference': 2}, {'id': 's2', 'preference': 4}, {'id': 's3', 'preference': 2}], [{'id': 'r1', 'capacity': 2}, {'id': 'r2', 'capacity': 4}])

Output: {'assignments': [{'room_id': 'r1', 'student_ids': ['s1', 's3']}, {'room_id': 'r2', 'student_ids': ['s2']}], 'unassigned': []}

Example 2

Input: allocate_rooms([{'id': 'alice', 'preference': 4}, {'id': 'bob', 'preference': 4}, {'id': 'cara', 'preference': 4}, {'id': 'dan', 'preference': 4}, {'id': 'erin', 'preference': 4}], [{'id': 'north', 'capacity': 4}])

Output: {'assignments': [{'room_id': 'north', 'student_ids': ['alice', 'bob', 'cara', 'dan']}], 'unassigned': ['erin']}

Example 3

Input: allocate_rooms([{'id': 's1', 'preference': 2}, {'id': 's2', 'preference': 4}, {'id': 's3', 'preference': 2}], [{'id': 'r4', 'capacity': 4}, {'id': 'r2', 'capacity': 2}, {'id': 'r3', 'capacity': 2}])

Output: {'assignments': [{'room_id': 'r4', 'student_ids': ['s2']}, {'room_id': 'r2', 'student_ids': ['s1', 's3']}, {'room_id': 'r3', 'student_ids': []}], 'unassigned': []}

Hints

Hint 1

Group the students by requested room size into two first-in-first-out lines so that input order is preserved within each size.

Hint 2

Walk through the rooms in order. For each room, repeatedly take a student from the line matching its capacity, stopping when the room is full or the line is empty.

Hint 3

Record every room in the output even if it received nobody. At the end, scan the original student list and collect those who were never placed, which keeps their input order.