1229 - Meeting Scheduler
Difficulty: Medium | Pattern: Two Pointers / Sorting | Company tags: Facebook, Amazon, Google
Problem Statement
Given the availability time slots arrays slots1 and slots2 of two people and a meeting duration, return the earliest time slot that works for both of them and is of duration duration.
If there is no common time slot that satisfies the requirements, return an empty array.
Note: Slots are [start, end] intervals. A valid meeting must be a continuous block of duration minutes that fits within both people's availability.
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: []
Approach: Sort + Two Pointers — O(n log n + m log m)
Key insight: Sort both slot arrays. Use two pointers to find overlapping intervals. For each overlap, check if it's long enough for the meeting.
def minAvailableDuration(slots1, slots2, duration):
slots1.sort()
slots2.sort()
i, j = 0, 0
while i < len(slots1) and j < len(slots2):
# Find intersection of slots1[i] and slots2[j]
start = max(slots1[i][0], slots2[j][0])
end = min(slots1[i][1], slots2[j][1])
if end - start >= duration:
return [start, start + duration]
# Advance the pointer with the earlier ending slot
if slots1[i][1] < slots2[j][1]:
i += 1
else:
j += 1
return []
Dry Run
slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration=8
After sort: already sorted.
| i | j | start | end | end-start | action |
|---|---|---|---|---|---|
| 0 | 0 | max(10,0)=10 | min(50,15)=15 | 5 lt 8 | slots2[0] ends first → j=1 |
| 0 | 1 | max(10,60)=60 | min(50,70)=50 | -10 lt 8 | slots1[0] ends first → i=1 |
| 1 | 1 | max(60,60)=60 | min(120,70)=70 | 10 gte 8 | return [60, 68] |
[60, 68] ✓
Edge Cases
- No overlap → return []
- Overlap exactly equal to duration → valid
- Multiple valid slots → return earliest (first found with sorted + two-pointer)
Complexity
- Time: O(n log n + m log m) for sorting
- Space: O(1) extra