Assemble DNA payload strings from tagged fragments
Company: Microsoft
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in string manipulation, sequence assembly, and labeled-graph path reconstruction—skills involved in ordering fragments by matching start/end tags and concatenating payloads—and it belongs to the Coding & Algorithms domain.
Part 1: Assemble a Single Forward DNA Chain
Constraints
- 0 <= len(fragments) <= 100000
- Each `start_tag`, `end_tag`, and `payload` is a string.
- For non-empty input, the fragments form one valid directed path with no branching.
- No tag has more than one outgoing fragment or more than one incoming fragment.
- The total length of all payload strings is at most 1000000.
Examples
Input: ([('XXX', 'ATG', 'Hello'), ('ATG', 'GCA', 'World'), ('GCA', 'TAG', '!'), ('TAG', 'YYY', 'Done')],)
Expected Output: 'HelloWorld!Done'
Explanation: The fragments are already in chain order.
Input: ([('B', 'C', '2'), ('A', 'B', '1'), ('C', 'D', '3')],)
Expected Output: '123'
Explanation: The correct chain is A -> B -> C -> D, so the payload order is 1, 2, 3.
Hints
- Think of each fragment as a directed edge from `start_tag` to `end_tag`.
- The first fragment starts at the tag that appears as a `start_tag` but never as an `end_tag`.
Part 2: Assemble a DNA Chain Traversable in Either Direction
Constraints
- 0 <= len(fragments) <= 100000
- Each `tag_a`, `tag_b`, and `payload` is a string.
- For non-empty input, the fragments form one simple undirected path with no branching.
- Each tag has undirected degree at most 2.
- The total length of all payload strings is at most 1000000.
Examples
Input: ([('XXX', 'ATG', 'Hello'), ('ATG', 'GCA', 'World'), ('GCA', 'TAG', '!'), ('TAG', 'YYY', 'Done')],)
Expected Output: 'HelloWorld!Done'
Explanation: The endpoints are XXX and YYY. Starting from XXX gives the forward payload order.
Input: ([('M', 'Z', 'middle'), ('A', 'M', 'start')],)
Expected Output: 'startmiddle'
Explanation: The endpoints are A and Z. Starting from A visits payloads start, then middle.
Hints
- Build an undirected adjacency list where each edge stores its payload.
- A chain endpoint is a tag with degree 1. Start from the smaller endpoint and walk without going back over the edge you just used.
Part 3: Assemble Multiple Forward DNA Chains
Constraints
- 0 <= len(fragments) <= 100000
- Each `start_tag`, `end_tag`, and `payload` is a string.
- The fragments form zero or more disjoint directed paths.
- No tag has more than one outgoing fragment or more than one incoming fragment.
- The total length of all payload strings is at most 1000000.
Examples
Input: ([('A', 'B', 'Hi'), ('C', 'D', 'Bye')],)
Expected Output: ['Bye', 'Hi']
Explanation: There are two one-fragment chains. The returned list is sorted lexicographically.
Input: ([('M', 'N', 'b'), ('A', 'B', 'x'), ('B', 'C', 'y'), ('N', 'O', 'c')],)
Expected Output: ['bc', 'xy']
Explanation: The chains are M -> N -> O producing bc, and A -> B -> C producing xy.
Hints
- Use indegree and outdegree to find the starting tag of each chain.
- After finding every start tag, traverse each chain exactly once and collect its payloads.