Coding interview problems โ€บ Intervals

๐Ÿ“… Intervals Easy

Meeting Rooms

Meeting Rooms explained: can one person attend every meeting? Sort by start and check neighbours, O(n log n) tested Python and a live visual.

Also searched as: can a person attend all meetings ยท check overlapping meetings ยท meeting rooms I

๐Ÿ“…

Try it live Free

Merge Intervals & Meeting Rooms

Lay the meetings on the Merge Intervals timeline: if any two fuse into one busy block, the person cannot attend them all.

Also useful: Sorting Race: Insertion vs Merge vs Quick

Open the playground โ†’

The problem

Given a list of meeting times [start, end], decide whether one person could attend all of them, meaning no two meetings overlap. A meeting ending at 10 and another starting at 10 do not clash.

Input[[0, 30], [5, 10], [15, 20]]
OutputFalse
Input[[7, 10], [2, 4]]
OutputTrue

The key insight

Sort the meetings by start time. If any two meetings overlap, then some meeting overlaps the one right after it in sorted order. So you only need to compare neighbours: does the next meeting start before the current one ends?

Brute force first: Comparing every pair of meetings for overlap is O(nยฒ).

Step-by-step approach

  1. Sort the meetings by start.
  2. For each adjacent pair, check whether next.start < current.end.
  3. If any pair overlaps, return False.
  4. Otherwise return True.

Python solution

PYTHON 3tested
def can_attend_meetings(intervals):
    intervals = sorted(intervals)
    for (s1, e1), (s2, e2) in zip(intervals, intervals[1:]):
        if s2 < e1:                  # next starts before this one ends
            return False
    return True

Complexity

TimeO(n log n)
SpaceO(n)

Sorting costs O(n log n); the neighbour check is O(n). sorted makes a copy, which is O(n) space.

Common mistakes

Frequently asked questions

What is the time and space complexity of Meeting Rooms?

O(n log n) time for the sort and O(n) or O(1) extra space depending on whether you sort a copy or in place.

Why is checking adjacent meetings enough?

After sorting by start, if meeting A overlaps a later meeting C, then the meeting B just after A starts no later than C, so it starts before A ends too. An overlap therefore always shows up between neighbours.

What if I need the minimum number of rooms?

That is Meeting Rooms II. Count +1 at each start and -1 at each end in time order, or keep a min-heap of end times; the peak count is the answer.

Preparing for placements?

Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.

All coding interview problems More Intervals Merge Intervals & Meeting Rooms lab Placement prep guides

Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.