AlgoMaster Logo

Meeting Scheduler

mediumFrequencyUpdated September 21, 2026

Understanding the Problem

We have two people, each with a list of time slots when they are free. We need to find the earliest window of at least duration minutes where both are simultaneously available.

A valid meeting window must lie within the overlap of one slot from person 1 and one slot from person 2. The problem reduces to finding overlapping pairs of slots, computing each overlap length, and returning the earliest overlap that is at least duration long.

The overlap of two slots [s1, e1] and [s2, e2] is [max(s1, s2), min(e1, e2)]. Its length is min(e1, e2) - max(s1, s2), which is positive only when the slots intersect. The slots are not given in sorted order, and each person's own slots never overlap each other. That structure (two lists of disjoint intervals) lets us avoid comparing every pair once both lists are sorted.

Key Constraints:

  • 1 <= slots1.length, slots2.length <= 10^4 → With up to 10,000 slots per person, an O(n m) brute force reaches 10^4 10^4 = 10^8 overlap checks, which is slow. Sorting plus a linear scan is O(n log n + m log m), which is comfortable here.
  • 0 <= slots1[i][0] < slots1[i][1] <= 10^9 → Time values reach a billion, so indexing an array by time value is not an option. We work with the intervals directly. The values still fit in a 32-bit signed integer, and start + duration reaches at most 10^9 + 10^6, which also fits, so no overflow guard is needed.

Approach 1: Brute Force (Check All Pairs)

Intuition

Try every combination of one slot from person 1 and one slot from person 2. For each pair, compute the overlap. If the overlap is at least duration long, it can host the meeting, so record it as a candidate. After checking all pairs, return the candidate with the earliest start time.

This checks every pair regardless of whether they could intersect, which is slow for large inputs, but it is simple and serves as a correctness baseline.

Algorithm

  1. Initialize result as an empty list to track the best answer so far.
  2. For each slot s1 in slots1:
    • For each slot s2 in slots2:
      • Compute the overlap: overlapStart = max(s1[0], s2[0]), overlapEnd = min(s1[1], s2[1]).
      • If overlapEnd - overlapStart >= duration, this pair can host the meeting.
      • If result is empty or overlapStart is earlier than the current best, update result = [overlapStart, overlapStart + duration].
  3. Return result.

Visualization and Code

Loading animation...

The next approach processes the slots in chronological order so it only compares slots that are near each other in time, which removes the quadratic blowup.

Approach 2: Sort + Two Pointers

Intuition

Sort both slot lists by start time, then walk through them with two pointers, the same way you merge two sorted arrays. At each step, compare the current slot from each list, compute their overlap, and check whether it is at least duration long. If it is, that is the answer. If not, advance the pointer whose slot ends earlier.

Algorithm

  1. Sort slots1 by start time.
  2. Sort slots2 by start time.
  3. Initialize two pointers i = 0 and j = 0.
  4. While i < slots1.length and j < slots2.length:
    • Compute overlapStart = max(slots1[i][0], slots2[j][0]).
    • Compute overlapEnd = min(slots1[i][1], slots2[j][1]).
    • If overlapEnd - overlapStart >= duration, return [overlapStart, overlapStart + duration].
    • Otherwise, advance the pointer whose current slot ends earlier. If slots1[i][1] < slots2[j][1], increment i. Otherwise, increment j.
  5. If no valid overlap was found, return an empty list.

Visualization and Code

Loading animation...

The two-pointer approach has the best time complexity for this problem. A common alternative merges both lists into a single sorted sequence and scans neighbors, which trades a little extra space for not having to track two pointers.

Approach 3: Heap-Based (Min-Heap on Start Time)

Intuition

Combine all slots from both people into one min-heap ordered by start time, dropping any slot shorter than duration because it can never host the meeting on its own. Then pull slots off the heap in order and compare each one with the previous slot.

If a slot prev and the next slot curr overlap by at least duration, that overlap is the answer. The two slots must come from different people: a single person's slots are disjoint, so two same-person slots in sorted order satisfy prev[1] < curr[0], which can never give a positive overlap. The first qualifying neighbor pair therefore gives a valid cross-person meeting, and because the heap yields slots in increasing start order, it is the earliest one.

Algorithm

  1. Create a min-heap (priority queue) ordered by start time.
  2. Add all slots from both lists to the heap, but skip any slot where end - start < duration.
  3. Pull the first slot off the heap. Call it prev.
  4. While the heap is not empty:
    • Pull the next slot curr from the heap.
    • If prev[1] >= curr[0] + duration, return [curr[0], curr[0] + duration].
    • Set prev = curr.
  5. If no valid overlap found, return an empty list.

Visualization and Code

Loading animation...