AlgoInsight

DSA problem

Meeting Scheduler

MediumIntervalsPattern: Intervalstop600GoogleFacebookAmazonMicrosoftLinkedIn

You are given the availability time slots of two people, slots1 and slots2, where each slot is [start, end], and a meeting duration. Return the earliest time interval of length duration during which both people are simultaneously available. If no such slot exists, return an empty array.

Each availability list is independent and unordered. The natural first step is to sort both lists by start time. Once sorted, the problem becomes a classic two-pointer sweep: at any moment we examine the earliest slot of each person and ask whether their overlap is long enough to host the meeting.

The overlap of two intervals [s1, e1] and [s2, e2] is [max(s1, s2), min(e1, e2)]. The overlap exists when max(s1, s2) <= min(e1, e2) and its length is the difference. If that length is at least duration, the earliest valid meeting starts at max(s1, s2) and ends duration units later. Otherwise we discard the slot that ends first (it cannot help us anymore) and advance its pointer.

This sweep visits each slot at most once after sorting, so the dominant cost is sorting at O((m + n) log(m + n)) time and O(1) extra space beyond the sort. The two-pointer pattern is robust against tricky cases: empty lists return empty, and large durations relative to available windows simply exhaust both pointers without finding a fit.

Examples

Example 1
Input: slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration = 8
Output: [60,68]
Explanation: Sorted slots overlap at [60,70] giving a window of length 10. Since 10 >= 8, the earliest valid meeting is [60, 60+8] = [60,68].
Example 2
Input: slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration = 12
Output: []
Explanation: Overlaps are [10,15] (length 5) and [60,70] (length 10). Neither reaches duration 12, so no meeting can be scheduled.
Example 3
Input: slots1 = [[0,100]], slots2 = [[5,15],[30,90]], duration = 30
Output: [30,60]
Explanation: Overlapping window [30,90] has length 60 which is at least 30; the earliest 30-minute meeting is [30,60].

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

arrayintervalstwo-pointerssorting

Browse all DSA problems