KatInterview

Conflict-Free Schedule Check for a Single Attendee

Greedy & IntervalsAuto-graded · 18 test cases
EasyAuto-gradedGreedy & Intervalsintervalssortinggreedy

Last 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 begin always conflict, since every slot has begin < finish.
  • Identical slots appearing twice conflict.
  • The function must return a boolean.

Constraints

  • 0 <= len(slots) <= 10,000
  • Each slot is [begin, finish] with 0 <= 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.