Data Structures & AlgorithmsModerateTCS NQT · sweep line

Problem: What is the minimum number of meeting rooms needed for intervals [0,30], [5,10], [15,20]? Run the sweep-line code.

Problem: What is the minimum number of meeting rooms needed for intervals [0,30], [5,10], [15,20]? Run the sweep-line code.
iv = [(0, 30), (5, 10), (15, 20)]
ev = sorted([(s, 1) for s, e in iv] + [(e, -1) for s, e in iv])
c = m = 0
for t, d in ev:
    c += d
    m = max(m, c)
print(m)
What is printed?
  1. A. 3
  2. B. 1
  3. C. 4
  4. D. 2

Correct answer: D. 2

Sorting puts an end event before a start event at the same time, so back-to-back meetings share a room. The peak overlap is 2 (during [5,10] inside [0,30]).

More Coding Interview Problems questions

Exams that ask this topic