Given everyone's busy calendar, find the slots when all of them are free. What do you sort, and what breaks at the boundaries?
Pool every busy interval, sort by start, and sweep with a cursor marking the end of the busy block so far. Any start beyond the cursor opens a common free slot. Use half-open intervals so back-to-back meetings leave no zero-length gap, and clip to the working day. Sorting costs O(N log N), or O(N log k) with a heap.
Answer
Short answer
Common free time is whatever is left after you merge intervals from every calendar into one busy timeline. Pool every busy interval, sort by start, and sweep with a cursor holding the end of the busy block built so far. Whenever an interval starts after the cursor, the gap between them is free for everybody. Treat intervals as half-open so back-to-back meetings leave no zero-length slot, and clip to the working day.
Was this answer helpful?
Likely follow-ups
- Return only slots of at least thirty minutes. Where does the filter go, and can it change the sweep?
- Find the slots where at least three of five people are free. Why does merging stop working?
- One person is double-booked with overlapping meetings in their own calendar. Does your code still hold?
- Attendees are in different time zones and some calendars use closed integer ranges. What do you normalise first?
Related questions
- Given a list of meetings with start and end times, tell me the minimum number of rooms needed to hold all of them.mediumAlso on intervals and sweep-line5 min
- Merge a list of overlapping intervals, and justify the key you sorted byeasyAlso on intervals and sweep-line4 min
- You are handed k sorted lists and asked for one sorted list. What is wrong with concatenating and sorting?mediumAlso on heaps6 min
- Numbers arrive one at a time and after each one you must answer what the Kth largest so far is. What do you keep, and what do you deliberately throw away?mediumAlso on heaps6 min