Meeting Rooms solution explained

Meeting Rooms asks whether one person can attend every meeting in a list, which is the same as asking whether any two meetings overlap. Sort the meetings by start time and compare each one with the meeting just before it: if any meeting starts before the previous one ends, the answer is no. Sorting dominates, so it runs in O(n log n).

By Published 7 min read

The problem, reworded

You are given a list of meetings, each with a start time and an end time, in no particular order. One person wants to go to all of them, start to finish. Decide whether that is possible. A meeting that ends at 10 and another that starts at 10 do not conflict: you walk out of one and into the next.

Schedules made up for this page, with answers from the code below
MeetingsAttend all?Why
[[9, 10], [13, 15], [10, 12]]trueSorted, they run 9 to 10, 10 to 12, 13 to 15, with only a shared endpoint
[[8, 11], [14, 16], [10, 12]]falseThe 10 o'clock meeting starts before the 8 o'clock one ends
[[4, 6], [4, 5]]falseSame start time
[[1, 2]]trueOne meeting cannot clash with itself
[]trueNothing to attend
Schedules made up for this page, with answers from the code below. Scroll sideways on a small screen.

Pin down three things first. Are the times half-open, so back-to-back meetings are fine? (Usually yes, and this page assumes it.) Is every meeting at least a moment long, with start before end? (Assumed here.) And are the times plain numbers, or clock strings you would have to convert first?

Why it is a warm-up favorite

Meeting Rooms is short enough to open a phone screen or to lead into a harder scheduling question in the same session. It checks whether you sort before you loop, whether you get the touching-endpoint comparison right, and whether you can explain why checking neighbors is enough. A tidy answer here buys time for the follow-up, which is usually the real question.

Brute force: compare every pair

Two half-open meetings [s1, e1) and [s2, e2) overlap exactly when s1 < e2 and s2 < e1: each starts before the other ends. Test that condition for every pair. It needs no sorting and is easy to get right, which makes it a solid reference for testing. With n meetings there are about n²/2 pairs.

def fits_one_calendar_slow(meetings):
    for i in range(len(meetings)):
        for j in range(i + 1, len(meetings)):
            (s1, e1), (s2, e2) = meetings[i], meetings[j]
            if s1 < e2 and s2 < e1:  # half-open overlap test
                return False
    return True
All pairs: O(n²) time, O(1) extra space.

Sort, then look only at neighbors

Sort by start time. Now suppose meetings A and C overlap, with B sitting between them in sorted order. B starts no earlier than A and no later than C. Since C starts before A ends, B also starts before A ends, so A and B already overlap. In other words, if any pair clashes, some adjacent pair clashes too. So one walk comparing each meeting with the previous one finds every conflict that matters.

def fits_one_calendar(meetings):
    ordered = sorted(meetings)
    for prev, cur in zip(ordered, ordered[1:]):
        if cur[0] < prev[1]:  # starts before the previous one ends
            return False
    return True
Python: sort, then compare neighbors. O(n log n) time.
function fitsOneCalendar(meetings) {
  const ordered = [...meetings].sort((x, y) => x[0] - y[0]);
  for (let k = 1; k < ordered.length; k++) {
    if (ordered[k][0] < ordered[k - 1][1]) return false;
  }
  return true;
}
JavaScript: copy before sorting, and pass a numeric comparator.

Two details in the JavaScript are worth saying aloud. Spreading into a new array keeps the caller's list in its original order. And the comparator matters: without it, the default sort compares values as text, which can put a meeting at 10 before one at 9.

Trace on the second example

Sorted by start: [8, 11], [10, 12], [14, 16]. Compare [10, 12] with [8, 11]: it starts at 10, which is before 11, so return false. The loop never reaches [14, 16].

Follow-up: bookings that arrive one at a time

A common extension turns the list into a stream. Each request asks to book a meeting, and you must accept it only if it fits around everything accepted so far. Re-sorting on every request would be wasteful. Keep the accepted meetings sorted by start, and for a new request find where it would go with binary search. Only two meetings can clash with it: the one just before that spot and the one just after.

from bisect import bisect_right

class Diary:
    def __init__(self):
        self.starts = []
        self.ends = []

    def book(self, start, end):
        k = bisect_right(self.starts, start)
        if k > 0 and self.ends[k - 1] > start:
            return False  # clashes with the meeting before it
        if k < len(self.starts) and self.starts[k] < end:
            return False  # clashes with the meeting after it
        self.starts.insert(k, start)
        self.ends.insert(k, end)
        return True
Python: sorted lists plus bisect. The search is O(log n); inserting into a Python list is O(n).
A run of booking requests through the class above
RequestAccepted?Reason
10 to 12yesFirst booking
14 to 15yesStarts after 12
12 to 14yesFits exactly between the two
11 to 13noStarts before the 10 to 12 meeting ends
9 to 10yesEnds as the first booking starts
15 to 16yesStarts as the 14 to 15 meeting ends
13 to 14noStarts inside the 12 to 14 meeting
A run of booking requests through the class above. Scroll sideways on a small screen.

In a language with a balanced tree map, such as a sorted map keyed by start time, both the lookup and the insert become logarithmic.

Complexity

n meetings
ApproachTimeExtra space
Every pairO(n²)O(1)
Sort, then neighborsO(n log n)O(n) for a sorted copy, or O(1) if you may sort in place
Online booking, sorted list plus bisectO(log n) search, O(n) insert per requestO(n)
Online booking, balanced treeO(log n) per requestO(n)
n meetings. Scroll sideways on a small screen.

Edge cases

  • An empty list or a single meeting: true.
  • Back-to-back meetings that share an endpoint: true under the half-open rule.
  • Two meetings with the same start: always a clash, whatever their ends.
  • One long meeting that contains several short ones: the first short one after it in sorted order already clashes.
  • Times given as clock strings: convert to minutes first, since comparing strings such as 9:30 and 10:00 as text gives the wrong order.

Mistakes to avoid

  • Comparing each meeting only with the first one, or with the meeting before it in the original unsorted order.
  • Using at-or-before instead of strictly-before for the clash test, which rejects back-to-back meetings.
  • Sorting by end instead of start without adjusting the argument. It can be made to work, but the neighbor proof above assumes start order.
  • Sorting the caller's list in place without saying so.

Where the interview goes next

  • Fewest rooms for all meetings. Instead of a yes or no, count the most meetings running at the same moment. Sort start and end times separately and sweep them, or keep a min-heap of end times, as sketched on the Merge Intervals page.
  • Report the first clashing pair. Return the two neighbors where the check fails.
  • Drop the fewest meetings so the rest fit. That is Non-overlapping Intervals.
  • Add one meeting to a free/busy calendar and merge it with what is there. That is Insert Interval.

Talking it through

Fictional example

How the explanation might go
Candidate
I'll treat times as half-open, so back-to-back is fine. Checking every pair is quadratic, so I'll sort by start instead.
Candidate
Once sorted, if any two meetings overlap, some neighboring pair overlaps too, because anything between them starts before the first one ends. So I just compare each meeting with the one before it.
Candidate
That's n log n for the sort and a linear scan. If you want the room count next, I'd sweep the starts and ends in time order.

Insert Interval adds one range to a calendar that is already clean. Non-overlapping Intervals removes the fewest meetings to make a schedule clean. Merge Intervals combines overlaps rather than rejecting them. The coding problems hub lists every walkthrough.

Rehearsing with Natively

On a Mac or Windows machine, Natively can read a scheduling problem like this from a screenshot in a practice session and help you get the touching-endpoint question and the neighbor argument straight before you code. It also writes a transcript of your mock and keeps notes, so you can reread whether you explained why neighbors are enough. The AI interview assistant page describes it further.

Questions people ask

How do you solve Meeting Rooms?

Sort the meetings by start time and compare each with the one before it. If any meeting starts before the previous one ends, one person cannot attend them all.

Why is comparing neighbors enough in Meeting Rooms?

After sorting by start, if two meetings overlap, any meeting between them starts before the first one ends, so an adjacent pair overlaps too.

What is the time complexity of Meeting Rooms?

O(n log n) for the sort and O(n) for the scan. Checking every pair without sorting is O(n²).

Do back-to-back meetings conflict in Meeting Rooms?

Usually not: times are treated as half-open, so a meeting ending at 10 and one starting at 10 both fit. Confirm the rule, since it decides whether the comparison is strict.

Where to go next

Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.

Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.

Keep reading

Put it on your own machine.

A live-conversation assistant that runs locally. Your conversations stay on your machine.

macOS 12+ · Windows 10/11

© 2026 Natively AI Private Limited

Source-available under the Natively Personal Use Source License v1.0 · IP geolocation by DB-IP