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?- A. 4
- B. 3
- C. 5
- 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
- Count the unordered pairs (i < j) of the array a = [1, 2, ..., 14] whose sum is divisible by 3. Run the code. python a=list(range(1,15))…
- Consider the permutation p = [4, 1, 7, 3, 8, 2, 6, 5]. Count its inversions, i.e. pairs of positions i < j with p[i] > p[j]. Run the code…
- Problem: A connected undirected graph has 7 vertices. Its BFS tree from vertex 0 has depths: vertex 0 at depth 0, vertices 1 and 2 at…
- Problem: A graph's vertices 0–4 are connected by edges 0-1 (1), 1-2 (1), 2-3 (1), 3-4 (1), 0-4 (5), 1-3 (3). What is the largest…
- Problem: In a directed graph on vertices 0–3, the arcs are 0→1, 1→2, 2→0, 2→3. The vertex 3 has no outgoing arcs. How many vertices can…
- Problem: A rooted tree has root 1 and children 1: [2, 3, 4], 3: [5], 4: [6, 7]. The number of nodes on the longest root-to-leaf path…