Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard

Problem: Maximum bipartite matching between workers (left) and jobs (right) with Kuhn's augmenting-path algorithm.

Problem: Maximum bipartite matching between workers (left) and jobs (right) with Kuhn's augmenting-path algorithm.
adj = {0: [0, 1], 1: [0], 2: [1, 2], 3: [2], 4: [2, 3]}
match = {}
def try_(u, seen):
    for v in adj[u]:
        if v in seen: continue
        seen.add(v)
        if v not in match or try_(match[v], seen):
            match[v] = u; return True
    return False
print(sum(try_(u, set()) for u in adj))
What is printed?
  1. A. 5
  2. B. 3
  3. C. 4
  4. D. 2

Correct answer: C. 4

There are only 4 jobs, so at most 4 matches. Augmenting paths re-route earlier choices: worker 1 needs job 0, pushing worker 0 to job 1, which pushes worker 2 to job 2, and so on. The result 4 equals the brute-force maximum.

More Coding Interview Problems questions

Exams that ask this topic