AO
Back

Merge Two Branches with the Fewest Commit Conflicts

Also reported as Merging Branches · Merge Two Strings with Minimum Conflicts

OAAsync OALast reported September 2026Low Frequency

Problem Overview

The round: the coding half of Amazon's online assessment, next to an AI-assisted debugging task in a code repository. Three candidates reported it between April and September 2026. Two of them describe the rules in full; nobody posted constraints or a worked example, so the examples here are ours, apart from the reported "zab" count. The function name is ours.

Problem

Two branches have commit histories primary and secondary, each a string of lowercase letters. Each letter is a commit's priority: a smaller letter is a higher priority (a highest, z lowest). Merge the two histories into one string that keeps each branch's own order. A conflict is any pair of commits in the merged string where a lower-priority commit comes before a higher-priority one; for example, "zab" has 2 conflicts (z before a, z before b). Return the minimum number of conflicts over all merges.

def minConflicts(primary: str, secondary: str) -> int: ...

Examples (ours)

primary = "za",  secondary = "b"     merges: "zab" 2, "zba" 3, "bza" 2   -> 2
primary = "acd", secondary = "b"     "abcd" has none                    -> 0
primary = "b",   secondary = "caa"   "caab" has 3 (c>a, c>a, c>b)       -> 3

Pairs inside one branch count too: they are in the merged string whatever you do, so they add the same amount to every merge. Two commits with the same letter have the same priority and never conflict.

What Reports Emphasize

What candidates report: Two candidates describe the same rules: keep each branch's order, a smaller letter is a higher priority, and a conflict is a lower-priority commit before a higher-priority one; one of them gives "zab" as having 2 conflicts.; One candidate rates it a medium-to-hard string DP.; One candidate enumerated merges with backtracking and concluded afterwards that it needed DP.

Outcomes: The backtracking candidate was rejected the next day.

Our note (not from the reports): DP over the two prefixes: a commit placed last conflicts with every already-placed commit of the other branch that has lower priority. Always taking the higher-priority head first is wrong.

Practice

Write your own against 7 test cases, or read the worked solution — approach, complexity, and code that runs.

Prepping for Amazon with friends?

Send them this page. It is free to read, no account needed.

More Amazon Questions

Free preview

Every question in the Amazon catalog gets this depth

What you just read — canonical solution, follow-up arc, what passing candidates actually did — exists for all 57 Amazon questions, refreshed monthly from new candidate reports.

$59/mo — or $50/mo with the 3-month pass · cancel anytime
Amazon · OA · Reported 3× across candidate reports
Is this helpful?