Data Structures & AlgorithmsModerateProduct Company SDE · Amazon/Microsoft-style OA
Problem: Remove the fewest meetings so that the rest do not overlap (meetings that only touch at an endpoint are fine).
Problem: Remove the fewest meetings so that the rest do not overlap (meetings that only touch at an endpoint are fine).
def remove_min(iv):
iv.sort(key=lambda x: x[1])
end, keep = float('-inf'), 0
for s, e in iv:
if s >= end:
keep += 1; end = e
return len(iv) - keep
print(remove_min([[1, 2], [2, 3], [3, 4], [1, 3]]), remove_min([[1, 10], [2, 3], [4, 5], [6, 7]]))What is printed?- A. 1 3
- B. 2 1
- C. 1 1
- D. 0 1
Correct answer: C. 1 1
Activity selection: sorting by END time and keeping each meeting that starts after the last kept end maximises the kept set. Second case: [2,3], [4,5], [6,7] are kept and only the long [1,10] goes. Sorting by start time would keep [1,10] first and remove 3. O(n log n).
More Coding Interview Problems questions
- Problem: Find the minimum number of meeting rooms needed for a list of meetings [start, end). python import heapq def rooms(iv)…
- 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…