Minimize Branch Merge Conflicts
AmazonSoftware Engineer · Online AssessmentLast updated
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.
You are given several feature branches that must all be merged into the main branch. Each branch contains a sequence of commits, and each commit modifies one or more file paths.
Implement minMergeConflicts(branches) -> int.
branches[i][j]is the list of file paths modified by thej-th commit in branchi.- You may choose any order in which to merge the branches.
- The first merged branch has zero conflicts.
- When merging a branch after some other branches have already been merged, a commit in the current branch is counted as a conflicting commit if it modifies at least one file path that has already been modified by any previously merged branch.
- A commit counts at most once, even if it touches multiple already-modified files.
- After a branch is merged, all files modified by all of its commits are considered modified for future merges.
Return the minimum possible total number of conflicting commits after merging all branches.
Example:
branches = [[['a'], ['b']], [['b'], ['c']], [['a', 'c']]]
One optimal order is branch 0, then branch 1, then branch 2:
- Merge branch
0:0conflicts. - Merge branch
1: commit['b']conflicts, commit['c']does not, so1conflict. - Merge branch
2: commit['a', 'c']conflicts, so1conflict.
The minimum total conflict count is 2.
Assume the number of branches is small enough for subset dynamic programming, for example n <= 15.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.