Meeting Scheduler - Problem

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.

The format of a time slot is an array of two elements [start, end] representing an inclusive time range from start to end.

It is guaranteed that no two availability slots of the same person intersect with each other. That is, for any two time slots [start1, end1] and [start2, end2] of the same person, either start1 > end2 or start2 > end1.

Input & Output

Example 1 — Basic Meeting Scheduling
$ Input: slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration = 8
Output: [60,68]
💡 Note: The earliest common slot is [60,70] which has length 10 ≥ 8, so we schedule the meeting from 60 to 68
Example 2 — No Common Slot
$ Input: slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration = 12
Output: []
💡 Note: The only overlap [60,70] has length 10 < 12, so no meeting can be scheduled
Example 3 — Multiple Overlaps
$ Input: slots1 = [[0,20],[50,80]], slots2 = [[10,30],[70,90]], duration = 5
Output: [10,15]
💡 Note: Two overlaps exist: [10,20] and [70,80], but [10,20] is earlier, so return [10,15]

Constraints

  • 1 ≤ slots1.length, slots2.length ≤ 104
  • slots1[i].length = slots2[i].length = 2
  • slots1[i][0] < slots1[i][1]
  • slots2[i][0] < slots2[i][1]
  • 1 ≤ duration ≤ 106

Visualization

Tap to expand
Meeting Scheduler - Optimal Solution INPUT slots1: Time Range: 0 -------- 210 [10,50] [60,120] [140,210] slots2: [0,15] [60,70] duration = 8 Timeline View: 0 60 120 210 P1 P2 Overlap at 60-70 slots1 = [[10,50],[60,120],[140,210]] slots2 = [[0,15],[60,70]], dur = 8 ALGORITHM STEPS 1 Sort Both Slots By start time ascending 2 Two Pointer Scan i=0 for slots1, j=0 for slots2 3 Find Intersection start=max(s1,s2), end=min(e1,e2) 4 Check Duration If end-start >= duration: found! Iteration at i=1, j=1: slots1[1] = [60, 120] slots2[1] = [60, 70] start = max(60,60) = 60 end = min(120,70) = 70 70 - 60 = 10 >= 8 [OK] Return [60, 60+8] = [60, 68] FINAL RESULT Meeting Scheduled: [60, 68] Visual Confirmation: 0 60 70 120 overlap 8 Meeting Earliest Valid Slot Found - Both available: 60 to 70 - Overlap duration: 10 mins - Need: 8 mins [OK] Output: [60, 68] Key Insight: Two-pointer technique on sorted intervals achieves O(m log m + n log n) time complexity. For each pair, compute intersection as [max(start1, start2), min(end1, end2)]. If intersection length >= duration, return [start, start + duration]. Move pointer with smaller end time forward. TutorialsPoint - Meeting Scheduler | Optimal Solution
Asked in
Google 25 Amazon 18 Microsoft 15 Facebook 12
32.0K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen