Data Structures & AlgorithmsModerateProduct Company SDE · Amazon/Microsoft-style OA

Problem: Find the minimum number of meeting rooms needed for a list of meetings [start, end).

Problem: Find the minimum number of meeting rooms needed for a list of meetings [start, end).
import heapq
def rooms(iv):
    iv.sort(); h = []
    for s, e in iv:
        if h and h[0] <= s: heapq.heapreplace(h, e)
        else: heapq.heappush(h, e)
    return len(h)
print(rooms([[0, 30], [5, 10], [15, 20], [10, 15], [25, 35]]))
What is printed?
  1. A. 3
  2. B. 2
  3. C. 1
  4. D. 4

Correct answer: B. 2

Sort by start; a min-heap of end times tells whether the earliest-ending room is free (end ≤ start, so [5,10) and [10,15) can share). [0,30) occupies one room all along; the others chain in a second room: 5–10, 10–15, 15–20, 25–35. O(n log n).

More Coding Interview Problems questions

Exams that ask this topic