← Questions

Fewest conflicts when merging branches

0
AmazonCodingLast asked Jan 2026

A coding interview question asked in Amazon's online assessment, last asked Jan 2026. It tests dynamic programming and bitmask.

Several feature branches must all be merged into the main branch, one after another, in an order you choose. Each branch is a sequence of commits, and each commit modifies a set of file paths. The order matters: a commit conflicts if it touches a file that an earlier-merged branch already modified. Return the fewest conflicting commits any merge order can produce.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • min_merge_conflicts(branches: list[list[list[str]]]) -> int — branches[i][j] is the list of paths modified by commit j of branch i.

Rules:

  • The first branch merged has no conflicts.
  • When a branch is merged, each of its commits counts as one conflict if it modifies at least one path already modified by any branch merged before it. A commit counts once however many such paths it touches.
  • After a branch is merged, every path any of its commits touched counts as modified.

Example

branches = [[["a"], ["b"]], [["b"], ["c"]], [["a", "c"]]]   ->  2
# order 0, 1, 2: branch 0 costs 0; branch 1's ["b"] conflicts, ["c"] doesn't (1);
# branch 2's ["a", "c"] conflicts (1). No order does better.

branches = [[["x"], ["y"], ["z"]], [["x", "y"]], [["q"]]]   ->  1
# merging branch 0 before branch 1 makes only ["x", "y"] conflict; branch 1 first costs 2.

branches = []   ->  0

Constraints

  • 0 <= n <= 15 branches; every branch has at least one commit.
  • A commit may modify no paths; a path repeated within a commit or branch counts once.

Clarifying questions

  • Do commits of the same branch conflict with each other? (No: only earlier-merged branches count.)
  • Can a commit have no paths?
  • How many distinct paths can there be in total?

What a strong answer covers

  • The key observation: the cost of merging a branch depends only on the set of branches already merged, not their order, so n! orders collapse into 2^n subsets.
  • Bitmask DP: dp[S] is the fewest conflicts after merging exactly the branches in S; dp[S | 1<<b] = min(dp[S | 1<<b], dp[S] + cost(b, files(S))). O(2^n · n · C) for C commits per branch.
  • Paths mapped to bit positions (or Python ints used as bitsets) so each commit test is one AND; the file union of each subset built from a smaller subset.
  • Why greedy (always merge the cheapest branch next) fails: a cheap branch can modify many files and make later merges expensive.

Follow-ups

  • Return an optimal order as well as the count.
  • n grows to 40. What do you do instead?
  • A conflict costs the number of conflicting files in the commit, not 1. What changes?

Where it's asked

  • Amazon · Online assessment · Last asked Jan 2026Coding problems plus a repository bug-fix section with a built-in assistant or a design scenario, then a work-style survey · AI only inside the bug-fix section's built-in assistant

What it tests

dynamic programming · bitmask

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding