Data Structures & AlgorithmsHardProduct Company SDE · Amazon/Microsoft-style OA

Problem: Given n meetings with values, choose non-overlapping ones (end < next start) to maximise total value – weighted interval scheduling.

Problem: Given n meetings with values, choose non-overlapping ones (end < next start) to maximise total value – weighted interval scheduling.
from bisect import bisect_left
def best(jobs):
    jobs.sort(key=lambda j: j[1])
    ends = [e for _, e, _ in jobs]
    dp = [0] * (len(jobs) + 1)
    for i, (s, e, v) in enumerate(jobs):
        j = bisect_left(ends, s)
        dp[i + 1] = max(dp[i], dp[j] + v)
    return dp[-1]
print(best([(1, 3, 50), (2, 4, 10), (3, 5, 40), (3, 6, 70)]), best([(1, 2, 5), (1, 3, 6), (2, 4, 6)]))
What is printed?
  1. A. 120 6
  2. B. 90 6
  3. C. 70 6
  4. D. 70 11

Correct answer: C. 70 6

Sort by end; dp[i + 1] = max(skip job i, take job i + best among jobs ending strictly before its start). bisect_left(ends, s) counts jobs with end < s, so meetings that merely touch (end 3, start 3) are NOT compatible. In the first set no two jobs are compatible, so the best single job, 70, wins; allowing touching would give 50 + 70 = 120. Likewise 5 + 6 = 11 needs touching. O(n log n).

More Coding Interview Problems questions

Exams that ask this topic