ProgrammingHardCodeforces-style · placement coding round

What is printed?

What is printed?
import heapq
def rooms(iv):
    iv = sorted(iv)
    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)]), rooms([(7, 10), (2, 4)]))
  1. A. 1 1
  2. B. 2 2
  3. C. 3 1
  4. D. 2 1

Correct answer: D. 2 1

The heap holds the end times of rooms in use. A meeting reuses a room when the earliest-finishing room is free, so the first list needs 2 rooms and the second needs 1.

More Python Programming questions

Exams that ask this topic