You are given the availability time slots arrays slots1
and slots2
of two people and a meeting duration duration
. Your task is to find the earliest time slot that both people can have a meeting of the given duration.
Each time slot is represented as an interval [start, end]
, representing an available time slot from start
to end
(inclusive).
Return the earliest time slot [start, end]
such that both people have availability for a meeting of the given duration
. If there is no solution, return an empty list []
.
Example 1:
Input: slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration = 8
Output: [60,68]
Example 2:
Input: slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration = 12
Output: []
Constraints:
1 <= slots1.length, slots2.length <= 104
slots1[i].length, slots2[i].length == 2
0 <= slots1[i][0] < slots1[i][1] <= 109
0 <= slots2[i][0] < slots2[i][1] <= 109
duration >= 1
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to the meeting scheduler problem involves checking every single possible time slot to see if it works for all attendees. We systematically explore all potential meeting times, regardless of how inefficient it might be. If a time slot works for everyone, we consider it a potential solution.
Here's how the algorithm would work step-by-step:
def meeting_scheduler_brute_force(availability, meeting_duration):
all_possible_slots = []
# Iterate through all possible start times.
for start_time in range(0, 1440 - meeting_duration + 1):
is_slot_available = True
# Check if the current slot works for all attendees.
for person_schedule in availability:
person_available = False
for available_slot in person_schedule:
if start_time >= available_slot[0] and start_time + meeting_duration <= available_slot[1]:
person_available = True
break
# If even one person is busy, move on.
if not person_available:
is_slot_available = False
break
# Save the time if everyone is available.
if is_slot_available:
all_possible_slots.append(start_time)
#Choose earliest time
if not all_possible_slots:
return None
else:
return all_possible_slots[0]
To efficiently schedule meetings, we focus on finding overlapping time slots. The key is to organize the meeting times to quickly identify common available periods for all attendees. By comparing meeting times in a specific order we can avoid unnecessary checks and quickly find suitable meeting times.
Here's how the algorithm would work step-by-step:
def meeting_scheduler(schedule):
merged_schedule = []
for person_schedule in schedule:
merged_schedule.extend(person_schedule)
# Sort all meeting times to process chronologically.
merged_schedule.sort()
available_slots = []
latest_end_time = 0
for meeting_time in merged_schedule:
start_time, end_time = meeting_time
# Check for available slots, identifying gaps in the schedule.
if start_time > latest_end_time:
available_slots.append((latest_end_time, start_time))
# Update the latest end time for overlap checks.
latest_end_time = max(latest_end_time, end_time)
return available_slots
Case | How to Handle |
---|---|
Both slots1 and slots2 are empty. | Return an empty list, as there are no available time slots for either person. |
One of slots1 or slots2 is empty. | Return an empty list, as a meeting requires availability from both people. |
duration is zero or negative. | Return an empty list or throw an exception since a meeting duration must be positive. |
No overlapping interval of at least duration exists. | Return an empty list when the algorithm iterates and finds no suitable meeting slot. |
Large duration that exceeds any possible overlap. | The algorithm will iterate and correctly return an empty list as there is no suitable meeting slot. |
Overlapping intervals exist, but none are long enough to accommodate duration. | The algorithm will check the length of each overlapping interval and skip it if shorter than duration. |
Input intervals are not sorted. | Sort both slots1 and slots2 by start time before finding overlaps. |
Integer overflow if start/end times are very large. | Use appropriate data types (e.g., long) to avoid overflow or check the overflow explicitly before calculations. |