Data Structures & AlgorithmsModerateProduct Company SDE · Amazon/Microsoft-style OA
Problem: Find the minimum number of meeting rooms needed for a list of meetings [start, end).
Problem: Find the minimum number of meeting rooms needed for a list of meetings [start, end).
import heapq
def rooms(iv):
iv.sort(); 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], [10, 15], [25, 35]]))What is printed?- A. 3
- B. 2
- C. 1
- D. 4
Correct answer: B. 2
Sort by start; a min-heap of end times tells whether the earliest-ending room is free (end ≤ start, so [5,10) and [10,15) can share). [0,30) occupies one room all along; the others chain in a second room: 5–10, 10–15, 15–20, 25–35. O(n log n).
More Coding Interview Problems questions
- Problem: A car starts with an empty tank at one of n petrol pumps on a circular route; pump i gives gas[i] and the trip to the next costs…
- Problem: Each child must get at least one sweet, and a child with a higher rating than a neighbour must get more than that neighbour. Find…
- Problem: Each position holds the maximum jump length from it. Return the minimum jumps to reach the last index. python def jumps(a): j =…
- Problem: Can you reach the last index if a[i] is the maximum jump from i? java static boolean canReach(int[] a) { int far = 0; for (int i…
- Problem: Coins of denominations {1, 7, 10} are available in unlimited supply. Pay 14 with the fewest coins. What does the greedy 'largest…
- Problem: Split a string into as many parts as possible so each letter appears in at most one part; return the part sizes. python def…