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?- A. 120 6
- B. 90 6
- C. 70 6
- 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
- Problem: Find the minimum cost to climb stairs where cost[i] is paid on stepping on stair i, moving 1 or 2 stairs, starting at stair 0 or…
- Problem: Count the distinct BST shapes that store keys 1..n. python def shapes(n): dp = [1] + [0] n for k in range(1, n + 1): dp[k] =…
- Problem: Count the number of ways to tile a 2 × n board with 2 × 1 dominoes, modulo 10⁹ + 7. cpp const long long MOD = 1e9 + 7; long long…
- Problem: Find the minimum number of perfect squares that sum to n. python def squares(n): dp = [0] + [float('inf')] n for i in range(1, n…
- Problem: Count the number of distinct subsequences of s that equal t. python def distinct(s, t): dp = [1] + [0] len(t) for ch in s: for j…
- Problem: Find the longest increasing path in a matrix (moving up/down/left/right to strictly larger values). python from functools import…