Early Access: 87 spots left.

Claim
Coding Interview PatternsMerge IntervalsConflicting Appointments

Problems

Conflicting Appointments

Medium·Tagsarraysorting

Problem Statement

Given an array of intervals representing `N` appointments, find out if a person can attend all the appointments. An appointment is represented by a `[start, end]` array. A person can attend all appointments if no two appointments overlap with each other.

Examples

Example 1
Input
intervals: [[1, 4], [2, 5], [7, 9]]
Timeline
124579[1, 4][2, 5][7, 9]
Output
"false"
Why
Since [1, 4] and [2, 5] overlap, a person cannot attend both of these appointments.
Example 2
Input
intervals: [[6, 7], [2, 4], [8, 12]]
Timeline
2467812[6, 7][2, 4][8, 12]
Output
"true"
Why
None of the appointments overlap, therefore a person can attend all of them.
Example 3
Input
intervals: [[4, 5], [2, 3], [3, 6]]
Timeline
23456[4, 5][2, 3][3, 6]
Output
"false"
Why
Since [4, 5] and [3, 6] overlap, a person cannot attend both of these appointments.

Constraints

  • 1 <= intervals.length <= 10000
  • intervals[i].length == 2
  • 0 <= start < end <= 100000

Hints

Stuck? Reveal a nudge toward the right pattern, one step at a time.

Hint 1
If the appointments are not sorted, you would have to compare every single appointment against every other appointment to ensure no overlaps exist.
Hint 2
Try sorting the appointments based on their start times first.
Hint 3
Once sorted, an appointment can only conflict with the appointment immediately following it. If `intervals[i].end > intervals[i+1].start`, you have a conflict!