Merge two ordered commit streams with the fewest priority inversions

Quick Overview

Merge two commit-priority strings while preserving each input order and minimizing inverted pairs. Account for equal priorities and unavoidable internal conflicts, and derive an efficient prefix-state dynamic program.

Merge two ordered commit streams with the fewest priority inversions

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Online Assessment

Two strings, `primary` and `secondary`, describe commit streams. Each lowercase letter is a commit priority, with alphabetically smaller letters representing higher priority. Merge the strings into one sequence while preserving the original relative order of every character from each input. A conflict is a pair of positions in which a lower-priority commit appears before a higher-priority commit. In letter terms, positions i and j conflict when `i < j` and the letter at i is greater than the letter at j. Return the minimum possible number of conflicts over all valid merges. ### Function Signature ```python def getMinimumConflicts(primary: str, secondary: str) -> int: ``` ### Rules - Every character occurrence from both strings appears exactly once in the merge. - Characters from either one string cannot be reordered relative to each other. - Equal letters have equal priority and do not conflict. - Return the minimum count, not a particular merged string. ### Constraints - `1 <= len(primary), len(secondary) <= 1000`. - Both inputs contain only lowercase ASCII letters. - The answer is at most `1999000`, which fits a signed 32-bit integer. ### Examples ```text Input: primary = "zc", secondary = "d" Output: 2 ``` The legal merges are `zcd`, `zdc`, and `dzc`. Their conflict counts are two, three, and two respectively. ```text Input: primary = "ab", secondary = "ab" Output: 0 ``` The merge `aabb` preserves both input orders and contains no conflicting pair.

Overview: Merge two commit-priority strings while preserving each input order and minimizing inverted pairs. Account for equal priorities and unavoidable internal conflicts, and derive an efficient prefix-state dynamic program.

Read the full Amazon Software Engineer interview experience this question came from

Amazon logo
Amazon
Nov 28, 2026
hardSoftware EngineerOnline AssessmentCoding & Algorithms
0
0

Two strings, primary and secondary, describe commit streams. Each lowercase letter is a commit priority, with alphabetically smaller letters representing higher priority. Merge the strings into one sequence while preserving the original relative order of every character from each input.

A conflict is a pair of positions in which a lower-priority commit appears before a higher-priority commit. In letter terms, positions i and j conflict when i < j and the letter at i is greater than the letter at j. Return the minimum possible number of conflicts over all valid merges.

Function Signature

def getMinimumConflicts(primary: str, secondary: str) -> int:

Rules

  • Every character occurrence from both strings appears exactly once in the merge.
  • Characters from either one string cannot be reordered relative to each other.
  • Equal letters have equal priority and do not conflict.
  • Return the minimum count, not a particular merged string.

Constraints

  • 1 <= len(primary), len(secondary) <= 1000 .
  • Both inputs contain only lowercase ASCII letters.
  • The answer is at most 1999000 , which fits a signed 32-bit integer.

Examples

Input: primary = "zc", secondary = "d"
Output: 2

The legal merges are zcd, zdc, and dzc. Their conflict counts are two, three, and two respectively.

Input: primary = "ab", secondary = "ab"
Output: 0

The merge aabb preserves both input orders and contains no conflicting pair.