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

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.

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: ('zc', 'd')

Expected Output: 2

Explanation: The optimal merge can start from either stream.

Input: ('ab', 'ab')

Expected Output: 0

Explanation: A monotone merge preserves both orders with no conflicts.

Hints

  1. When appending a commit, only larger letters already placed create new conflicts.
  2. A partial merge uses a prefix of each stream.

Merge two ordered commit streams with the fewest priority inversions

hardAmazonSoftware Engineer

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.

Examples

Example 1
Input
('zc', 'd')
Output
2
Notes
The optimal merge can start from either stream.
Example 2
Input
('ab', 'ab')
Output
0
Notes
A monotone merge preserves both orders with no conflicts.

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.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def getMinimumConflicts(primary: str, secondary: str) -> int:
Sample call
getMinimumConflicts('zc', 'd')
Language
Case 1
Input
('zc', 'd')
Expected
2
Case 2
Input
('ab', 'ab')
Expected
0

Browse More Questions

Show the approach

Approach

For each letter and each input prefix, precompute the number of characters in that prefix that are strictly greater than the letter. Consider a state with the first i primary and first j secondary characters merged. If the last character came from primary, it creates one new conflict for every larger letter in the preceding i−1 primary and j secondary characters. Those counts depend only on the prefixes, regardless of their interleaving. The secondary-last case uses the analogous prefixes.

Use dynamic programming over (i, j), taking the smaller of the two last-character transitions whenever both are available. The optimal cost obeys this recurrence because every allowed merge has exactly one of those final choices, and appending to an optimal predecessor incurs the same new conflicts as appending to any other predecessor. Initialize states with an empty stream by appending from the nonempty one. Store one row of costs, reading the old entry for the primary-last case and the current row's previous entry for the secondary-last case.

Time complexity:
O(n × m + 26 × (n + m))
Space complexity:
O(26 × (n + m))