Meeting Rooms

Asked atGoogle
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below

The problem

Given an array of meeting time intervals where intervals[i] = [starti, endi].

Determine if a person could attend all meetings.

Input: intervals = [[1, 5], [3, 8], [6, 10], [12, 15]] Output: false Explanation: Overlapping meetings exist at [1,5] and [3,8], making it impossible to attend all.

Input: intervals = [[2, 6], [7, 9], [10, 14], [15, 18]] Output: true Explanation: No overlapping meetings, so all can be attended.

Input: intervals = [[0, 4], [4, 9], [10, 13], [14, 17]]

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

cpp

class Solution {
public:
    bool canAttendMeetings(vector<vector<int>>& intervals) {
        // Your code goes here
    }
};

java

class Solution {
    public boolean canAttendMeetings(int[][] intervals) {
        // Your code goes here
    }
}

python

class Solution:
    def canAttendMeetings(self, intervals):
        # Your code goes here

javascript

class Solution {
    canAttendMeetings(intervals) {
        // Your code goes here
    }
}

csharp

public class Solution
{
    public bool canAttendMeetings(int[][] intervals)
    {
        // Your code goes here
    }
}

go

func canAttendMeetings(intervals [][]int) bool {
	// Your code goes here
}
Stuck? Show a way to structure it+
  1. 01Sort intervals by start time.
  2. 02Compare each start with the previous end.
  3. 03Reject on an overlap.
  4. 04Update the active end.
  5. 05Handle zero or one interval.

Reference answer

Then expect these follow-ups

  • How do you return a conflicting pair?

    Tests: index preservation

  • Can a sweep-line solve this too?

    Tests: alternative approaches

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question