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?- A. 3
- B. 1
- C. 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
- Problem: How many inversions (pairs i < j with a[i] > a[j]) does a = [2, 0, 2, 1, 1, 0] have? Run the code. python a = [2, 0, 2, 1, 1, 0]…
- Problem: How many integers x in [1, 50] are perfect sixth powers (x = y^6 for an integer y)? Run the code. python print(sum(1 for x in…
- Problem: Merging ropes of lengths 2, 3, 4 costs the sum of the two lengths merged each time, always merging the two shortest. What is the…
- Problem: How many subsets (including the empty one) of {1, 2, 3, 4, 5} have a sum divisible by 4? Run the code. python print(sum(1 for m…
- Problem: What does the following print? It takes the maximum of every window of size 2 in [4, 2, 12, 11, -5]. python a = [4, 2, 12, 11…
- Problem: What is the sum of the number of 1-bits in the binary representations of 1, 2, ..., 20? Run the code. python…