Find available reservation time slots
PinterestData Scientist · OnsiteLast updated
This question evaluates a candidate's ability to reason about time-interval data and temporal normalization, testing competencies in interval manipulation, conflict detection, and algorithmic problem-solving for scheduling contexts.
Problem
You are building an API to show which time slots are available for booking at a restaurant.
You are given:
- The restaurant’s opening hours (per day), and
- A list of existing reservations (per day), each with a start and end time.
Opening hours and reservations are provided in a nested dictionary structure.
Input
Assume times are in the same timezone and represented as minutes-from-midnight or as HH:MM strings that can be converted to minutes.
hours[day] = [open_time, close_time]reservations[day] = [[start1, end1], [start2, end2], ...]
Example shape:
hours = {
"Mon": ["11:00", "22:00"],
"Tue": ["11:00", "22:00"],
}
reservations = {
"Mon": [["12:00", "12:30"], ["18:00", "19:15"]],
"Tue": [],
}
Task
Write a function that returns, for each day, the list of available time intervals within opening hours that are not blocked by existing reservations.
Clarify/handle these cases:
- Reservations may be unsorted.
- Reservations may touch endpoints (e.g., one ends at 13:00 and another starts at 13:00).
- If reservations overlap, they should be merged before computing availability.
Output
Return a nested dictionary:
available[day] = [[free_start1, free_end1], [free_start2, free_end2], ...]
(Optionally, if a booking duration D minutes is provided, return only free intervals of length >= D, or return discrete start times valid for a D-minute reservation—state your choice.)
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.