Data Structures & AlgorithmsHardProduct Company SDE · greedy scheduling OA
Problem: Jobs are given as (deadline, profit): (2,100), (1,19), (2,27), (1,25), (3,15). Each job takes one unit slot and must finish by its deadline. Run the greedy code below. What is printed?
Problem: Jobs are given as (deadline, profit): (2,100), (1,19), (2,27), (1,25), (3,15). Each job takes one unit slot and must finish by its deadline. Run the greedy code below. What is printed?
jobs = [(2, 100), (1, 19), (2, 27), (1, 25), (3, 15)]
jobs.sort(key=lambda j: -j[1])
slot = [False] * 4
tot = 0
for d, p in jobs:
for t in range(min(d, 3), 0, -1):
if not slot[t]:
slot[t] = True
tot += p
break
print(tot)What is printed?- A. 161
- B. 142
- C. 134
- D. 157
Correct answer: B. 142
Greedy by profit places 100 in slot 2, 27 in slot 1 (slot 2 is taken), skips 25 and 19 (no free slot by deadline 1), and places 15 in slot 3. The total is 100 + 27 + 15 = 142.
More Coding Interview Problems questions
- Problem: Compute C(10, 3) mod 7 using Lucas' theorem, where 7 is prime. What is the result?
- Problem: What is 3^100 mod 10?
- Problem: How many positive divisors does 2^4 3^2 5 7 have?
- Problem: Minimum insertions of brackets to make the string '(()))(' valid, found by this greedy scan? python s = '(()))(' bal = 0 ins = 0…
- Problem: How many contiguous subarrays of [1, 2, 1, 2, 1, 3, 1] contain exactly 2 odd numbers? python a = [1, 2, 1, 2, 1, 3, 1] k = 2 cnt…
- Problem: How many subsets of {1, 2, 3, 4, 5, 6, 7, 8} have a sum of exactly 10? python cnt = 0 for m in range(1 << 8): if sum(i + 1 for i…