Skip to content
Preptima
mediumCodingEntryMidSenior

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.

5 min readUpdated 2026-10-09Target archetype: Big Tech, Enterprise Captive, Product Startup

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