253. Meeting Rooms II
mediumAsked at LinkedInFind the minimum number of meeting rooms needed to schedule all intervals without overlap — LinkedIn applies this algorithm to allocate recruiter calendar slots and to detect overlapping employment periods in member profiles, one of their most-cited interval problems.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given an array of meeting time intervals where intervals[i] = [start_i, end_i], return the minimum number of conference rooms required to hold all the meetings simultaneously.
Constraints
1 <= intervals.length <= 10^40 <= start_i < end_i <= 10^6
Examples
Example 1
intervals = [[0,30],[5,10],[15,20]]2Explanation: [0,30] overlaps with [5,10] requiring a second room; [15,20] fits in the room freed by [5,10].
Example 2
intervals = [[7,10],[2,4]]1Approaches
1. Sorted start + end arrays (two-pointer)
Separate starts and ends into two sorted arrays. Use two pointers: if next start < current end, a new room is needed; otherwise the earliest room frees up. O(n log n) for the sort, O(n) scan.
- Time
- O(n log n)
- Space
- O(n)
function minMeetingRoomsTwoPtr(intervals) {
const starts = intervals.map(i => i[0]).sort((a,b)=>a-b);
const ends = intervals.map(i => i[1]).sort((a,b)=>a-b);
let rooms = 0, maxRooms = 0, e = 0;
for (let s = 0; s < starts.length; s++) {
if (starts[s] < ends[e]) {
rooms++;
} else {
e++;
}
maxRooms = Math.max(maxRooms, rooms);
}
return maxRooms;
}Tradeoff:
2. Min-heap of end times — canonical
Sort by start time. Use a min-heap of room end-times. For each meeting, if its start >= heap minimum (earliest room frees), pop that room and push the new end; otherwise push a new room end. Heap size at the end = rooms used.
- Time
- O(n log n)
- Space
- O(n)
class MinHeap {
constructor(){this.h=[];}
push(v){this.h.push(v);this._up(this.h.length-1);}
pop(){const top=this.h[0];const last=this.h.pop();if(this.h.length){this.h[0]=last;this._down(0);}return top;}
peek(){return this.h[0];}
size(){return this.h.length;}
_up(i){while(i>0){const p=(i-1)>>1;if(this.h[i]<this.h[p]){[this.h[i],this.h[p]]=[this.h[p],this.h[i]];i=p;}else break;}}
_down(i){const n=this.h.length;while(true){let s=i,l=2*i+1,r=2*i+2;if(l<n&&this.h[l]<this.h[s])s=l;if(r<n&&this.h[r]<this.h[s])s=r;if(s===i)break;[this.h[i],this.h[s]]=[this.h[s],this.h[i]];i=s;}}
}
function minMeetingRooms(intervals) {
if (!intervals.length) return 0;
intervals.sort((a,b) => a[0]-b[0]);
const heap = new MinHeap();
for (const [start, end] of intervals) {
if (heap.size() && heap.peek() <= start) heap.pop();
heap.push(end);
}
return heap.size();
}Tradeoff:
LinkedIn-specific tips
LinkedIn interviewers often reframe this as 'given a list of employment date ranges from a member profile, find the maximum concurrent jobs.' The algorithm is identical — name that connection. The min-heap approach is preferred because it models the actual resource-allocation logic (earliest-freed room gets reused). The two-pointer trick is elegant but harder to explain intuitively; use it only if you can articulate why the sorted ends array works as a stand-in for the heap.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Meeting Rooms II and other LinkedIn interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →