Design a meeting room reservation API
UberSoftware Engineer · OnsiteLast updated
This problem tests your ability to design an in-memory scheduling system that efficiently manages room availability and handles time-interval overlaps.
Meeting Room Reservation API
Design an in-memory API to manage reservations for a set of meeting rooms in an office building. You need to provide functionality to add rooms and book them for specific time slots.
Requirements
Implement a class MeetingScheduler with the following methods:
add_room(room_id: str) -> None: Adds a new room to the system.reserve(room_id: str, start_time: int, end_time: int) -> bool: Attempts to reserve the given room fromstart_timetoend_time(exclusive). ReturnsTrueif the booking is successful,Falseotherwise.
Constraints
start_timeandend_timeare integers representing time units (e.g., hours or timestamps).- The same room cannot have overlapping bookings.
- Assume a reasonable number of rooms and bookings for an in-memory solution.
Example
scheduler = MeetingScheduler()
scheduler.add_room("Room_A")
# Returns True
scheduler.reserve("Room_A", 9, 11)
# Returns False (overlap: 10-12 overlaps with 9-11)
scheduler.reserve("Room_A", 10, 12)
# Returns True
scheduler.reserve("Room_A", 11, 13)
Edge Cases
- Booking a room that does not exist.
- An end time that is less than or equal to a start time.
- Back-to-back bookings (e.g., 9-10 and 10-11 should be allowed).
Hints
Hint 1
Use a dictionary to map room IDs to a list of existing reservations.
Hint 2
Store reservations as sorted tuples or objects representing the [start, end) intervals to simplify overlap detection.
Hint 3
Consider using binary search (or a library equivalent) to find the insertion point and check for overlaps with neighbors efficiently.
Follow-up questions
- How would you modify the API to find the first available room across all rooms for a requested time slot?
- What data structure would you use if you needed to support cancelling a reservation efficiently?
- How would you adapt this design to handle concurrent requests in a multi-threaded environment?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.