Find the shortest DNA sequence that contains every fragment as a substring

Quick Overview

Given up to eight DNA fragments, build the shortest sequence that contains every fragment as a substring and return the lexicographically smallest one when several tie. The problem tests string overlap computation, handling fragments contained in others, and searching over fragment orders with dynamic programming.

Find the shortest DNA sequence that contains every fragment as a substring

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

You are given a list of DNA `fragments` (strings consisting of characters `'A'`, `'C'`, `'G'`, `'T'`). Reconstruct the shortest possible DNA sequence that contains all the given fragments as substrings. The reconstruction should be done by finding overlaps between fragments and merging them optimally. Fragments that are complete substrings of other fragments should be ignored. If multiple shortest sequences exist, return the lexicographically smallest one. ### Function Signature ```python def reconstruct_dna(fragments: list[str]) -> str: ``` ### Rules - Every fragment must appear in the result as a contiguous substring. - A fragment that is a substring of another fragment, including a repeated copy of the same fragment, is covered by that other fragment and adds no length. - Ties between shortest sequences are broken by ordinary string comparison, so at the first position where two candidates differ, `'A' < 'C' < 'G' < 'T'`. ### Constraints - `1 <= len(fragments) <= 8` - `1 <= len(fragments[i]) <= 100` - `fragments[i]` consists only of characters `'A'`, `'C'`, `'G'`, `'T'`. ### Examples ```text Input: fragments = ["ATCG", "CGTA", "TAGC"] Output: "ATCGTAGC" ``` `"ATCG"` and `"CGTA"` overlap by 2 characters (`"CG"`), so merging them gives `"ATCGTA"`. Then `"ATCGTA"` and `"TAGC"` overlap by 2 characters (`"TA"`), giving a sequence of length 8. No other order of the three fragments overlaps by more than 2 characters in total. ```text Input: fragments = ["AAAA", "AAAT", "AATG", "ATGC"] Output: "AAAATGC" ``` Each consecutive pair overlaps by 3 characters: `"AAAA"` + `"AAAT"` gives `"AAAAT"`, adding `"AATG"` gives `"AAAATG"`, and adding `"ATGC"` gives `"AAAATGC"`, length 7. Two different fragments of length 4 can share at most 3 characters, so no sequence is shorter. ```text Input: fragments = ["CA", "AC", "C"] Output: "ACA" ``` `"C"` is inside `"CA"`, so it is ignored. The two orders of `"AC"` and `"CA"` give `"ACA"` and `"CAC"`, both of length 3, and `"ACA"` is lexicographically smaller.

Overview: Given up to eight DNA fragments, build the shortest sequence that contains every fragment as a substring and return the lexicographically smallest one when several tie. The problem tests string overlap computation, handling fragments contained in others, and searching over fragment orders with dynamic programming.

Microsoft logo
Microsoft
Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

You are given a list of DNA fragments (strings consisting of characters 'A', 'C', 'G', 'T'). Reconstruct the shortest possible DNA sequence that contains all the given fragments as substrings.

The reconstruction should be done by finding overlaps between fragments and merging them optimally. Fragments that are complete substrings of other fragments should be ignored.

If multiple shortest sequences exist, return the lexicographically smallest one.

Function Signature

def reconstruct_dna(fragments: list[str]) -> str:

Rules

  • Every fragment must appear in the result as a contiguous substring.
  • A fragment that is a substring of another fragment, including a repeated copy of the same fragment, is covered by that other fragment and adds no length.
  • Ties between shortest sequences are broken by ordinary string comparison, so at the first position where two candidates differ, 'A' < 'C' < 'G' < 'T' .

Constraints

  • 1 <= len(fragments) <= 8
  • 1 <= len(fragments[i]) <= 100
  • fragments[i] consists only of characters 'A' , 'C' , 'G' , 'T' .

Examples

Input: fragments = ["ATCG", "CGTA", "TAGC"]
Output: "ATCGTAGC"

"ATCG" and "CGTA" overlap by 2 characters ("CG"), so merging them gives "ATCGTA". Then "ATCGTA" and "TAGC" overlap by 2 characters ("TA"), giving a sequence of length 8. No other order of the three fragments overlaps by more than 2 characters in total.

Input: fragments = ["AAAA", "AAAT", "AATG", "ATGC"]
Output: "AAAATGC"

Each consecutive pair overlaps by 3 characters: "AAAA" + "AAAT" gives "AAAAT", adding "AATG" gives "AAAATG", and adding "ATGC" gives "AAAATGC", length 7. Two different fragments of length 4 can share at most 3 characters, so no sequence is shorter.

Input: fragments = ["CA", "AC", "C"]
Output: "ACA"

"C" is inside "CA", so it is ignored. The two orders of "AC" and "CA" give "ACA" and "CAC", both of length 3, and "ACA" is lexicographically smaller.