Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon/Microsoft-style OA
Problem: An investor starts with capital w and may finish at most k projects; project i needs capital[i] to start and adds profits[i] when done. Maximise the final capital: repeatedly pick the most profitable affordable project (sorted list + max-heap).
Problem: An investor starts with capital w and may finish at most k projects; project i needs capital[i] to start and adds profits[i] when done. Maximise the final capital: repeatedly pick the most profitable affordable project (sorted list + max-heap).
import heapq
def max_capital(k, w, profits, capital):
projects = sorted(zip(capital, profits)); h = []; i = 0
for _ in range(k):
while i < len(projects) and projects[i][0] <= w:
heapq.heappush(h, -projects[i][1]); i += 1
if not h: break
w -= heapq.heappop(h)
return w
p, c = [3, 5, 2, 8], [0, 3, 1, 6]
print(max_capital(2, 1, p, c), max_capital(3, 1, p, c), max_capital(2, 0, [4, 6], [1, 2]))What is printed?- A. 9 16 0
- B. 6 17 0
- C. 9 17 10
- D. 9 17 0
Correct answer: D. 9 17 0
Capital never decreases, so affordable projects stay affordable: push them into a max-heap as w grows and always take the best. Start w = 1: affordable profits 3, 2 → take 3 (w = 4); now the 5-profit project (needs 3) unlocks → w = 9. With k = 3 the 8-profit project (needs 6) unlocks → 17. With w = 0 nothing is affordable → 0. O(n log n + k log n).
More Coding Interview Problems questions
- Problem: A single CPU receives tasks (enqueue time, duration). When idle it starts the available task with the shortest duration (ties…
- Problem: Walk along buildings of given heights; climbing up by d needs d bricks or one ladder (going down is free). Return the furthest…
- Problem: Find the smallest range [lo, hi] that contains at least one number from each of k sorted lists (min-heap of one pointer per list…
- Problem: Choose the root(s) that minimise the height of an unrooted tree. Repeatedly strip all current leaves; the last 1 or 2 nodes are…
- Problem: Count the strongly connected components of a directed graph with Kosaraju's algorithm (DFS finishing order, then DFS on the…
- Problem: Stones sit on grid points; a stone may be removed if another remaining stone shares its row or column. Maximum removable = stones…