Meeting Rooms II solution explained

To find the fewest rooms that can host a set of meetings, sort the start times and the end times separately, then walk the starts in order: every start takes a room, and every end that has already passed by that moment gives one back. The largest number of rooms in use at once is the answer, and the Meeting Rooms II solution runs in O(n log n) time because of the two sorts.

By Published 7 min read

The task, restated

You manage a floor of identical meeting rooms. You receive a list of meetings for one day, each with a start time and an end time. A room can host only one meeting at a time, and a meeting that ends at 11 frees its room for one that starts at 11. Return the smallest number of rooms that lets every meeting happen as scheduled.

Examples made up for this page (times are hours)
MeetingsRoomsWhy
[[13, 15], [9, 10], [14, 16], [10, 11]]213 to 15 and 14 to 16 overlap; the morning pair is back to back
[[8, 12], [9, 10], [11, 13], [9, 13]]3At 9:30 three meetings are running: 8 to 12, 9 to 10 and 9 to 13
[[1, 4], [1, 4], [1, 4]]3Identical meetings each need their own room
[[1, 2], [2, 3], [3, 4]]1Each meeting ends exactly when the next starts
[]0No meetings, no rooms
Examples made up for this page (times are hours). Scroll sideways on a small screen.

Two questions to ask before coding. First, does a meeting ending at 11 conflict with one starting at 11? Most versions say no, and the code below assumes that. Second, are the times integers or could they be any numbers? The algorithm does not care, but it tells you whether a counting array over the clock is an option.

How interviewers use it

This problem often follows a warm-up that asks whether one person can attend every meeting, which is a single sort and a neighbor check. Meeting Rooms II raises the bar: you have to see that the question is really "what is the peak overlap?" and then find a way to measure the peak without comparing every pair. It is a common pick for scheduling, booking and infrastructure teams because it maps directly onto capacity questions such as servers, parking spaces or call agents.

  • Return which room each meeting gets, not just the count. Use the heap version and store the room id with each end time.
  • Meetings arrive one at a time and you must answer after each. A balanced tree or sorted map of +1 and -1 events keyed by time lets you update and query the peak.
  • Times are small integers, such as minutes in a day. A difference array of length 1,440 gives a linear-time answer.
  • Some rooms are larger than others. Now it becomes an assignment problem; say so and discuss a greedy approach and its limits.

Brute force: count overlaps at every start

The number of rooms in use only goes up when a meeting starts, so the peak must happen at some start time. For each meeting's start, count how many meetings are running at that instant (started at or before it, ending after it). The largest count is the answer. That is n checks of n meetings, O(n²) time and O(1) extra space. It is a fine first thing to say and a good test oracle.

def rooms_by_counting(meetings):
    best = 0
    for s, _ in meetings:
        busy = sum(1 for a, b in meetings if a <= s < b)
        best = max(best, busy)
    return best
Brute force: O(n²). Correct, and handy for testing the fast version.

The efficient approach: sweep over sorted starts and ends

You do not need to know which meeting ends; you only need to know how many have ended by a given time. So split the data. Sort all start times into one list and all end times into another. Walk through the starts in order with a second pointer on the ends. Before you count a new start, release every end time that is less than or equal to it, one room each. Then take a room for the new meeting and record the peak.

The <= in the release loop is what makes back-to-back meetings share a room. If the interviewer says touching meetings conflict, change it to <.

def min_rooms(meetings):
    starts = sorted(s for s, _ in meetings)
    ends = sorted(e for _, e in meetings)
    rooms = best = 0
    j = 0
    for s in starts:
        while j < len(ends) and ends[j] <= s:
            rooms -= 1
            j += 1
        rooms += 1
        best = max(best, rooms)
    return best
Python: two sorted lists and two pointers. O(n log n) time, O(n) space.
function minRooms(meetings) {
  const starts = meetings.map((m) => m[0]).sort((a, b) => a - b);
  const ends = meetings.map((m) => m[1]).sort((a, b) => a - b);
  let rooms = 0, best = 0, j = 0;
  for (const s of starts) {
    while (j < ends.length && ends[j] <= s) {
      rooms--;
      j++;
    }
    rooms++;
    best = Math.max(best, rooms);
  }
  return best;
}
JavaScript: the comparator matters, because the default sort compares numbers as text.

The heap version, as a cross-check

Sort meetings by start and keep a min-heap holding the end time of each room's current meeting. If the earliest end is at or before the new start, that room is free: pop it. Push the new end. The heap's final size is the room count. It runs in the same O(n log n) and is the better base when you must report room assignments. We tested both versions against the brute force on 3,000 random schedules. The Merge Intervals walkthrough, which this problem extends, uses the same heap idea as a follow-up.

import heapq

def min_rooms_heap(meetings):
    finishing = []
    for s, e in sorted(meetings):
        if finishing and finishing[0] <= s:
            heapq.heappop(finishing)
        heapq.heappush(finishing, e)
    return len(finishing)
Min-heap of end times, used here only to confirm the sweep's answers.

Time and space

Complexity for n meetings
VersionTimeExtra space
Count overlaps at each startO(n²)O(1)
Sorted starts and ends, two pointersO(n log n)O(n) for the two lists
Min-heap of end timesO(n log n)O(n) for the heap
Difference array over a small integer clock of size TO(n + T)O(T)
Complexity for n meetings. Scroll sideways on a small screen.

Edge cases

  • An empty list needs zero rooms.
  • One meeting needs one room.
  • Several identical meetings need one room each.
  • Back-to-back meetings share a room under the half-open rule.
  • A long meeting that covers many short ones: the long one holds its room the whole time.
  • Unsorted input, which is why both lists are sorted even if the example looks ordered.

Mistakes to avoid

  • Returning the room count at the end of the sweep instead of the peak seen during it.
  • Sorting only the starts and pairing each start with its own end, which breaks the two-pointer idea.
  • Using < where the problem expects back-to-back meetings to share, or the reverse.
  • Merging overlapping intervals and counting the merged groups. That answers a different question: [10, 11] and [9, 13] form one group but need two rooms.
  • Sorting numbers in JavaScript without a comparator.

How to explain it out loud

Fictional example

A clear walkthrough in about a minute
Candidate
The number of rooms I need is the most meetings running at the same instant, so I want the peak overlap.
Candidate
The slow way checks every start against every meeting, which is n squared. But I do not care which meeting ends, only how many have ended, so I can sort starts and ends separately.
Candidate
I walk the starts. Before taking a room, I give back one room for every end at or before this start. I track the maximum. Two sorts make it n log n, and I am treating back-to-back meetings as compatible.

Merge Intervals is the natural warm-up: same input, but it asks for the union of the ranges rather than the peak. The heap idea returns in Top K Frequent Elements. For a very different kind of cancellation, where every value meets its twin and disappears, see Single Number.

Where Natively fits

When you practice on a Mac or Windows PC, Natively can read a scheduling problem from a screenshot and suggest the sweep, including the question about meetings that touch. It also transcribes your mock interview, so you can check afterward whether you said "peak overlap" before you started typing, which is the sentence interviewers listen for. See the AI interview assistant for what it does.

Questions people ask

How do you solve Meeting Rooms II?

Sort the start times and end times separately. Walk the starts; before each one, free a room for every end at or before it, then take a room and track the maximum in use.

What is the time complexity of Meeting Rooms II?

O(n log n) for sorting, with a linear sweep afterward. The extra space is O(n) for the sorted lists or the heap.

Should Meeting Rooms II use a heap?

A min-heap of end times works and gives the same O(n log n). It is the better choice if you also need to say which room each meeting uses.

Do back-to-back meetings need separate rooms?

Usually not: a meeting ending at 11 frees the room for one starting at 11. Confirm with the interviewer, since it changes one comparison.

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