Data Structures & AlgorithmsVery hardProduct company OA
Problem: Assign interns to projects; intern u may take only projects in adj[u], each project at most one intern. Maximum bipartite matching with augmenting paths (Kuhn) versus 'each intern grabs the first free project'.
Problem: Assign interns to projects; intern u may take only projects in adj[u], each project at most one intern. Maximum bipartite matching with augmenting paths (Kuhn) versus 'each intern grabs the first free project'.
def kuhn(nL, adj):
match = [-1] * 10
def try_(u, seen):
for v in adj[u]:
if v in seen: continue
seen.add(v)
if match[v] == -1 or try_(match[v], seen):
match[v] = u; return True
return False
return sum(try_(u, set()) for u in range(nL))
def greedy(nL, adj):
used, c = set(), 0
for u in range(nL):
for v in adj[u]:
if v not in used: used.add(v); c += 1; break
return c
adj = {0: [0, 1], 1: [0], 2: [2, 1], 3: [2], 4: [3, 4], 5: [4]}
print(kuhn(6, adj), greedy(6, adj))What is printed?- A. 5 5
- B. 5 4
- C. 4 4
- D. 6 4
Correct answer: B. 5 4
Kuhn tries to place u; if the project is taken it recursively asks the current owner to move to another project – an augmenting path. Interns 0 and 1 compete for project 0 and 1 has no alternative, so 0 must move to project 1. Interns 2 and 3 then compete for project 2 (2's other option, project 1, is now taken), so only one of them is placed. Interns 4 and 5 resolve like 0 and 1. Maximum 5. Greedy gives 0 project 0 and 4 project 3, blocking 1 and 5 → 4. O(V·E).
More Coding Interview Problems questions
- Problem: For every warehouse in a tree network, compute the sum of distances to all other warehouses with two passes (rerooting)…
- Problem: Count integers in [1, N] divisible by at least one of the given numbers using inclusion–exclusion with LCMs (N up to 10⁹, so no…
- Problem: Compute C(n, r) mod a SMALL prime p = 7 when n ≥ p: Lucas' theorem versus factorials with Fermat inverses. python p = 7 fact =…
- Problem: Count ways to climb n stairs with steps of 1 or 3, modulo 10⁹ + 7, using a 3 × 3 matrix power so that n up to 10¹⁸ is fine…
- Problem: Peak number of simultaneous bookings with a sweep line. In the first call intervals are CLOSED [s, e] (a booking ending at 3 and…
- Problem: Total length covered by a set of intervals, and the length covered by EXACTLY one interval, using coordinate compression and a…