AlgoInsight

DSA problem

Meeting Rooms

EasyIntervalsPattern: Intervalstop600AmazonGoogleMicrosoftBloombergFacebook

Given an array of meeting time intervals intervals where intervals[i] = [start_i, end_i], determine if a single person could attend all the meetings. Two meetings conflict whenever they overlap in time — that is, one starts strictly before another finishes.

Meetings touching at an exact boundary do not conflict: if one ends at 10 and another starts at 10, both can be attended by the same person. This open-closed convention is critical for the comparison used in the solution.

A naive O(n^2) check compares every pair of meetings for overlap; this works but is wasteful. Sorting changes the problem dramatically. Once the intervals are sorted by start time, the only pair that can possibly overlap with meeting i is its immediate predecessor in the sorted order, because anything earlier ended even sooner.

The optimal approach therefore sorts by start time and performs a single linear sweep, returning false the moment any meeting starts before the previous one ends. If the sweep completes, every neighboring pair is compatible, so the answer is true. Sorting dominates the cost at O(n log n) with O(1) extra space beyond the sort.

Examples

Example 1
Input: intervals = [[0,30],[5,10],[15,20]]
Output: false
Explanation: After sorting by start time the list is [[0,30],[5,10],[15,20]]. [5,10] starts at 5 which is before 30 (the end of [0,30]), so the meetings overlap and cannot all be attended.
Example 2
Input: intervals = [[7,10],[2,4]]
Output: true
Explanation: After sorting by start the list is [[2,4],[7,10]]. The second meeting starts at 7 which is at or after 4, so the meetings do not overlap.
Example 3
Input: intervals = [[5,8],[8,10]]
Output: true
Explanation: Touching at the boundary is not a conflict: [5,8] ends exactly when [8,10] starts, so both can be attended.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

arrayintervalssorting

Browse all DSA problems