Data Structures & AlgorithmsVery hardTCS CodeVita · Microsoft SDE OA

Meetings are given as half-open intervals [(0,30),(5,10),(15,20),(12,25),(28,35)]. What is the minimum number of meeting rooms needed so that no two overlapping meetings share a room? Run the sweep code.

Meetings are given as half-open intervals [(0,30),(5,10),(15,20),(12,25),(28,35)]. What is the minimum number of meeting rooms needed so that no two overlapping meetings share a room? Run the sweep code.
ev=sorted([(s,1) for s,e in [(0,30),(5,10),(15,20),(12,25),(28,35)]]+[(e,-1) for s,e in [(0,30),(5,10),(15,20),(12,25),(28,35)]])
cur=mx=0
for t,d in ev:
    cur+=d; mx=max(mx,cur)
print(mx)
What is printed?
  1. A. 4
  2. B. 3
  3. C. 5
  4. D. 2

Correct answer: B. 3

The minimum number of rooms is the peak number of simultaneously running meetings, found by sweeping start and end events with ends first at equal times. Counting overlapping pairs is unrelated to the peak, and subtracting one is an off-by-one.

More Coding Interview Problems questions

Exams that ask this topic