KatInterview

First Open Meeting Window Across All Employees

Greedy & IntervalsModel answer
MediumModel answerGreedy & Intervalsintervalsheapmerge

Last updated

Sweep merged busy intervals to find the earliest gap of a required duration that fits within every employee's working hours.

Scheduling a Group Meeting

You are building a calendar tool that suggests the earliest available meeting time for a group. Each person has a defined work window for the day and a set of times they are already committed.

Given that information, your job is to find the earliest moment t such that the block [t, t + d) fits entirely within every person's work window and does not collide with any of their existing commitments.

Inputs

find_first_open_slot(schedules, work_windows, d)

  • schedules: a list of lists. schedules[i] is a sorted, non-overlapping list of busy intervals for person i, each expressed as [start, end) in minutes from midnight.
  • work_windows: a list of pairs [start, end) in minutes from midnight, one per person.
  • d: the required meeting length in minutes.

Return the earliest valid start time t, or -1 if no such block exists within a single day (minutes 0 to 1440).

Example

schedules    = [[[100, 200]], [[150, 250]]]
work_windows = [[0, 500], [0, 500]]
d            = 60

# Output: 0
# [0, 60) is free for everyone and falls inside both work windows.

Constraints

  • 0 <= d <= 1440
  • Each person's busy list is already sorted with no overlapping entries.
  • Up to 1,000 people.
  • Total busy intervals across all people is M.
  • Intervals are half-open: [start, end) means the minute end is free.

What is expected

Walk through your approach before coding. Explain how you handle the shift intersection, how you merge intervals from multiple people efficiently, and cover edge cases. Include a time and space complexity analysis.

Hints

Hint 1

Before looking at busy times at all, figure out the range of minutes where a meeting is even legal. A slot outside any one person's work window is automatically invalid — what does that mean for the combined valid range?

Hint 2

You don't need to know who is busy, only whether anyone is. Think about what structure lets you sweep all busy intervals in time order without flattening them into a single list first.

Hint 3

Track a single 'cursor' representing the earliest minute that is still a candidate start. When you encounter a busy block, ask: is there a gap before it wide enough? If not, how far forward should the cursor jump?