Conflict-Free Schedule Check for a Single Attendee
Greedy & IntervalsAuto-graded · 18 test casesLast updated
Tests sorting intervals and scanning adjacent pairs to detect overlaps under half-open interval rules, where touching endpoints are allowed.
Conflict-Free Schedule Check for a Single Attendee
You are handed a calendar made up of booked time slots. Each slot is a two-element list [begin, finish] of integers. A slot occupies the half-open range from begin up to, but not including, finish.
Decide whether one person could sit through every slot on the calendar without ever needing to be in two places at once.
Two slots conflict when their occupied ranges share some moment in time. Because finish is excluded, a slot that begins at the exact moment another one finishes does not conflict with it. For example, [1, 2] and [2, 3] are compatible.
The slots may be given in any order.
Task
Return True if no two slots conflict, and False otherwise.
Rules and edge cases
- An empty calendar can always be attended, so return
True. - A calendar with a single slot returns
True. - Two slots with the same
beginalways conflict, since every slot hasbegin < finish. - Identical slots appearing twice conflict.
- The function must return a boolean.
Constraints
0 <= len(slots) <= 10,000- Each slot is
[begin, finish]with0 <= begin < finish <= 1,000,000
Examples
Example 1
Input: can_attend_all_slots([[0, 30], [5, 10], [15, 20]])
Output: False
Example 2
Input: can_attend_all_slots([[7, 10], [2, 4]])
Output: True
Hints
Hint 1
Comparing every pair of slots works but is quadratic. Think about what ordering would make conflicts easy to spot.
Hint 2
After ordering the slots by start time, if any slot conflicts with an earlier one, it must already conflict with the slot directly before it.
Hint 3
Compare each slot's begin with the previous slot's finish. Since finish is exclusive, only a strictly smaller begin signals a conflict.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Sort the slots by start time (sorting the pairs lexicographically also works, with ties broken by finish). Then walk through the sorted list once, comparing each slot's begin to the previous slot's finish. If begin < previous finish, the two slots share a moment, so return False. If the scan completes without finding such a pair, return True.
The comparison is strict because finish is exclusive: a slot starting exactly at the previous finish is allowed.
Why it is correct
- A reported conflict is real. After sorting, the previous slot's begin is no later than the current slot's begin. If the current begin is also before the previous finish, the current slot starts while the previous one is still running, so they overlap.
- No reported conflict means no conflict at all. If every adjacent pair satisfies
previous finish <= current begin, then combined withbegin < finishfor each slot we get the chainb0 < f0 <= b1 < f1 <= b2 < f2 .... The slots are therefore laid out one after another with no overlap, and this includes non-adjacent pairs. - Ties on begin are caught naturally: two slots with the same begin sit next to each other after sorting, and the later one's begin is less than the earlier one's finish.
Complexity
- Time: O(n log n) for the sort plus O(n) for the scan.
- Space: O(n) for the sorted copy, which leaves the input untouched.
Reference solution
from typing import List
def can_attend_all_slots(slots: List[List[int]]) -> bool:
ordered = sorted(slots)
for i in range(1, len(ordered)):
if ordered[i][0] < ordered[i - 1][1]:
return False
return True