Skip to main content

Meeting Rooms - Solution & Explanation

EasyPremiumFree on FleetCodeArraySorting6 min readAsked at: Amazon, Microsoft, Apple +7
Practice this problem

Problem Statement

Given an array of meeting time intervals where intervals[i] = [starti, endi], determine if a person could attend all meetings.

 

Example 1:

Input: intervals = [[0,30],[5,10],[15,20]]
Output: false

Example 2:

Input: intervals = [[7,10],[2,4]]
Output: true

 

Constraints:

  • 0 <= intervals.length <= 104
  • intervals[i].length == 2
  • 0 <= starti < endi <= 106

Approach Overview

Problem Overview: You’re given an array of meeting time intervals where intervals[i] = [start, end]. The goal is simple: determine whether a single person can attend every meeting. If any two meetings overlap in time, attending all of them becomes impossible.

Approach 1: Brute Force Pair Comparison (O(n²) time, O(1) space)

The direct approach checks every pair of meetings to see if their time ranges intersect. For two intervals [s1, e1] and [s2, e2], an overlap exists if s1 < e2 and s2 < e1. Iterate through the array with two nested loops and test each pair. If any overlap is detected, return false; otherwise all meetings are compatible.

This method works but scales poorly. With n meetings you perform roughly n² comparisons, which becomes inefficient for larger inputs. It’s useful as a baseline to demonstrate understanding of interval overlap logic but rarely the preferred interview solution.

Approach 2: Sorting by Start Time (O(n log n) time, O(1) extra space)

A more efficient solution sorts the intervals by their start times using a standard sorting algorithm. After sorting, overlapping meetings must appear next to each other. Iterate once through the sorted list and compare the current meeting’s start time with the previous meeting’s end time.

If intervals[i].start < intervals[i-1].end, the meetings overlap and the person cannot attend both. If no such conflict appears during the scan, the schedule is valid. Sorting costs O(n log n), and the subsequent pass is O(n), giving a total time complexity of O(n log n). The scan itself uses constant extra memory aside from the sort implementation.

The key insight: once intervals are ordered by start time, only adjacent intervals need comparison. Any overlap will be revealed immediately during the linear scan.

This pattern appears frequently in interval problems. Sorting first simplifies reasoning and avoids expensive pairwise checks. Problems involving schedules, calendars, or ranges often combine arrays with sorting and linear sweeps.

Recommended for interviews: The sorting approach is what interviewers expect. Mention the brute force method briefly to show you understand overlap detection, then optimize by sorting and performing a single pass. That demonstrates both problem decomposition and knowledge of common interval strategies.

Solution

We sort the meetings based on their start times, and then iterate through the sorted meetings. If the start time of the current meeting is less than the end time of the previous meeting, it indicates that there is an overlap between the two meetings, and we return false. Otherwise, we continue iterating.

If no overlap is found by the end of the iteration, we return true.

The time complexity is O(n times log n), and the space complexity is O(log n), where n is the number of meetings.

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Pair ComparisonO(n²)O(1)Small input sizes or when demonstrating basic overlap logic before optimization
Sorting + Linear ScanO(n log n)O(1) extra (depends on sort)General case and expected interview solution for interval scheduling checks

Video Solution

Meeting Rooms - Leetcode 252 - Python • NeetCode • 131,755 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Meeting Rooms easy or hard?
Meeting Rooms is classified as an Easy problem on LeetCode with an acceptance rate around 59%. The challenge mainly involves recognizing that sorting intervals simplifies overlap detection, a pattern that appears in many interval-based interview questions.
Meeting Rooms Python/Java solution
In Python or Java, sort the intervals using the built-in sort function with the start time as the key. Then iterate from the second interval onward and check if the current start is less than the previous end. If it is, return false; otherwise continue until the array ends.
How to solve Meeting Rooms in O(n)?
A true O(n) solution is not possible for arbitrary unsorted intervals because you must first order them by time to reliably detect overlaps. Sorting requires O(n log n). Once sorted, the overlap detection step itself runs in O(n) with a simple linear scan.
What is the best approach for Meeting Rooms?
The best approach sorts the meeting intervals by start time and then performs a single linear scan to detect overlaps. After sorting, compare each meeting's start time with the previous meeting's end time. If any start time is smaller than the previous end, the meetings overlap. This runs in O(n log n) time and O(1) extra space.
Is Meeting Rooms asked at Google/Amazon/Meta?
Meeting Rooms is a common interval scheduling question used in interviews at companies like Google, Amazon, Meta, and other tech firms. It tests understanding of interval overlap detection, sorting strategies, and reasoning about time ranges.
What data structure is used in Meeting Rooms?
The problem primarily uses arrays to store meeting intervals. The key technique is sorting the array based on start times, followed by a sequential scan to compare adjacent intervals and detect overlaps.
What is the time complexity of Meeting Rooms?
The optimal solution runs in O(n log n) time due to sorting the intervals by their start times. After sorting, checking for overlaps requires only a single O(n) pass through the array. The brute force alternative takes O(n²) time because it compares every pair of intervals.

Ready to solve this problem?

Practice Meeting Rooms with our built-in code editor and test cases.

Practice on FleetCode