Minimize Branch Merge Conflicts
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Online Assessment
Overview: This question evaluates understanding of combinatorial optimization, set operations, and order-dependent state management, requiring reasoning about how commit file modifications accumulate across branch merges.
Constraints
- 1 <= number of branches n <= 15 (small enough for subset/bitmask DP).
- Each branch has at least one commit; a commit modifies zero or more file paths.
- File paths are arbitrary strings; the same path may appear across different branches and commits.
- Duplicate paths within a commit or branch are treated as a single file (idempotent).
- The answer is the minimum total conflicting-commit count over all merge orders.
Examples
Input: ([[['a'], ['b']], [['b'], ['c']], [['a', 'c']]],)
Expected Output: 2
Explanation: Worked example. Order 0->1->2: branch 0 is 0 conflicts; branch 1's ['b'] conflicts (1); branch 2's ['a','c'] conflicts (1). Total 2, the minimum over all orderings.
Input: ([],)
Expected Output: 0
Explanation: No branches to merge, so zero conflicts.
Hints
- Only the SET of already-merged branches matters for future cost, not the order they were merged in — the union of their modified files is order-independent. This collapses n! orderings into 2^n subsets.
- Encode each distinct file path as a bit. Then each commit and each branch is an integer bitmask, and 'does this commit touch an already-modified file?' is a single bitwise AND that is non-zero.
- Let dp[S] = min conflicts to have merged exactly the branches in subset S. Transition: for each branch b not in S, dp[S | (1<<b)] = min(..., dp[S] + (commits of b whose mask intersects files(S))). Answer is dp[(1<<n)-1].
- Greedy (always merge the branch adding the fewest new conflicts) fails: merging a branch enlarges the modified-file set and raises FUTURE costs. The DP weighs both effects.