Meeting Rooms - Problem

Imagine you're a busy professional trying to organize your calendar for the day. You have a list of meeting time intervals and need to determine if you can attend all meetings without any conflicts.

Given an array intervals where intervals[i] = [starti, endi] represents the start and end times of meeting i, return true if you can attend all meetings, or false if there are any overlapping meetings.

Goal: Check if any two meetings overlap in time.
Input: Array of meeting intervals (each interval has start and end time)
Output: Boolean value indicating if all meetings can be attended

Two meetings overlap if one starts before the other ends. For example, meetings [1,3] and [2,4] overlap because meeting 2 starts at time 2, which is before meeting 1 ends at time 3.

Input & Output

example_1.py โ€” No conflicts
$ Input: intervals = [[0,30],[5,10],[15,20]]
โ€บ Output: false
๐Ÿ’ก Note: The second meeting [5,10] overlaps with the first meeting [0,30] because it starts at time 5, which is before the first meeting ends at time 30. Similarly, [15,20] also overlaps with [0,30].
example_2.py โ€” Sequential meetings
$ Input: intervals = [[7,10],[2,4]]
โ€บ Output: true
๐Ÿ’ก Note: These two meetings do not overlap. The first meeting [2,4] ends at time 4, and the second meeting [7,10] starts at time 7, so there's no conflict.
example_3.py โ€” Edge case touching
$ Input: intervals = [[1,5],[5,8],[8,12]]
โ€บ Output: true
๐Ÿ’ก Note: These meetings are back-to-back but don't overlap. The first ends exactly when the second starts, which is allowed since you can attend consecutive meetings.

Visualization

Tap to expand
Meeting Room Conflict DetectionStep 1: Unsorted meetings[0,30][5,10][15,20]Step 2: Sort by start time[0,30][5,10][15,20]Step 3: Timeline visualization0510152030[0,30] - Long meeting[5,10][15,20]Step 4: Conflict detected!Meeting [5,10] starts at 5, but [0,30] doesn't end until 305 < 30 โ†’ Overlap detected โ†’ Return falseโœ“ Optimal Solution:1. Sort: O(n log n)2. Scan: O(n)Total: O(n log n)
Understanding the Visualization
1
Unsorted Chaos
Meetings are given in random order, making conflicts hard to spot
2
Sort by Start Time
Arrange meetings chronologically like a proper schedule
3
Linear Conflict Check
Check each meeting against the previous one for overlaps
4
Immediate Detection
Stop as soon as we find any overlap and return false
Key Takeaway
๐ŸŽฏ Key Insight: After sorting meetings by start time, we only need to check consecutive meetings for overlaps, dramatically reducing the number of comparisons needed!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

O(n log n) for sorting + O(n) for checking consecutive meetings = O(n log n) overall

n
2n
โšก Linearithmic
Space Complexity
O(1)

If we can sort in-place, otherwise O(n) for creating a sorted copy

n
2n
โœ“ Linear Space

Constraints

  • 0 โ‰ค intervals.length โ‰ค 104
  • intervals[i].length == 2
  • 0 โ‰ค starti < endi โ‰ค 106
  • Each interval represents a valid meeting time
Asked in
Google 48 Amazon 35 Microsoft 28 Meta 22 Apple 18
52.0K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen