Translate Open Reading Frames in a DNA Sequence into Protein Strings

Quick Overview

Scan a DNA string for open reading frames that begin at ATG and end at the first in-frame stop codon, then translate each one into a protein string with the standard codon table. It tests careful string scanning, reading-frame bookkeeping, and keeping the scan linear on long inputs.

Translate Open Reading Frames in a DNA Sequence into Protein Strings

Company: Spacex

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a string `sequence` of nucleotides made of the characters `A`, `T`, `C` and `G`, detect open reading frames (ORFs) and translate them into protein strings. An open reading frame begins with the start codon `ATG` and ends with a stop codon (`TAA`, `TAG` or `TGA`). A codon is a triplet of three consecutive nucleotides. The start codon `ATG` translates to `M` (methionine). Scan the sequence from left to right. When a start codon is found, read consecutive codons and translate each one to its amino acid using the standard genetic codon table. If a stop codon is encountered, the protein is complete: add it to the result and resume scanning after the stop codon. If the sequence ends before a stop codon is found, discard that partial reading frame. Each nucleotide can belong to only one reading frame. Return a list of all valid translated protein strings in the order their start codons appear. ### Function Signature ```python def translate_orfs(sequence: str) -> list[str]: ``` ### Rules - Look for `ATG` at every index, one position at a time, so a start codon can sit at any offset. - From a start codon at index `i`, read codons at `i`, `i + 3`, `i + 6` and so on. The ORF ends at the first of these codons that is a stop codon. Stop codons in other frames, including ones that overlap the start codon, are ignored. - The protein is the translation of every codon from the start codon up to the stop codon. The stop codon adds no letter, and an `ATG` inside an ORF is just another `M`. - After a completed ORF, scanning resumes at the index right after its stop codon, so completed ORFs never overlap. - If the frame runs out (fewer than 3 nucleotides left) before a stop codon, the ORF is discarded and scanning resumes at `i + 1`. Nucleotides of a discarded frame can still belong to a later ORF. - Return an empty list when there is no complete ORF. The standard codon table, grouped by the amino acid letter each codon produces: ```text F: TTT TTC L: TTA TTG CTT CTC CTA CTG I: ATT ATC ATA M: ATG V: GTT GTC GTA GTG S: TCT TCC TCA TCG AGT AGC P: CCT CCC CCA CCG T: ACT ACC ACA ACG A: GCT GCC GCA GCG Y: TAT TAC H: CAT CAC Q: CAA CAG N: AAT AAC K: AAA AAG D: GAT GAC E: GAA GAG C: TGT TGC W: TGG R: CGT CGC CGA CGG AGA AGG G: GGT GGC GGA GGG Stop: TAA TAG TGA ``` ### Constraints - `1 <= len(sequence) <= 10**5` - `sequence` consists only of the characters `A`, `T`, `C`, `G`. - Only complete ORFs (with both a start and a stop codon) are included in the result. ### Examples ```text Input: sequence = "ATGAAATTTCCCTAGATGTGGTAAGGG" Output: ["MKFP", "MW"] ``` The first ORF reads ATG, AAA, TTT, CCC and stops at TAG, giving MKFP, and scanning resumes at index 15. The second reads ATG, TGG and stops at TAA, giving MW, and the trailing GGG has no start codon. ```text Input: sequence = "AAATGATGCCCTAGTTT" Output: ["MMP"] ``` The first ATG starts at index 2, and its frame reads ATG, ATG, CCC and then the stop TAG, so the protein is MMP. The TGA at index 3 overlaps the start codon in another frame, so it does not end the ORF, and the trailing TTT has no start codon. ```text Input: sequence = "ATGCATGGCCTAG" Output: ["MA"] ``` The ATG at index 0 reads ATG, CAT, GGC, CTA and runs out of sequence before any stop, so it is discarded and scanning resumes at index 1. The ATG at index 4 reads ATG, GCC and then the stop TAG, which gives MA.

Overview: Scan a DNA string for open reading frames that begin at ATG and end at the first in-frame stop codon, then translate each one into a protein string with the standard codon table. It tests careful string scanning, reading-frame bookkeeping, and keeping the scan linear on long inputs.

Spacex logo
Spacex
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a string sequence of nucleotides made of the characters A, T, C and G, detect open reading frames (ORFs) and translate them into protein strings.

An open reading frame begins with the start codon ATG and ends with a stop codon (TAA, TAG or TGA). A codon is a triplet of three consecutive nucleotides. The start codon ATG translates to M (methionine).

Scan the sequence from left to right. When a start codon is found, read consecutive codons and translate each one to its amino acid using the standard genetic codon table. If a stop codon is encountered, the protein is complete: add it to the result and resume scanning after the stop codon. If the sequence ends before a stop codon is found, discard that partial reading frame. Each nucleotide can belong to only one reading frame.

Return a list of all valid translated protein strings in the order their start codons appear.

Function Signature

def translate_orfs(sequence: str) -> list[str]:

Rules

  • Look for ATG at every index, one position at a time, so a start codon can sit at any offset.
  • From a start codon at index i , read codons at i , i + 3 , i + 6 and so on. The ORF ends at the first of these codons that is a stop codon. Stop codons in other frames, including ones that overlap the start codon, are ignored.
  • The protein is the translation of every codon from the start codon up to the stop codon. The stop codon adds no letter, and an ATG inside an ORF is just another M .
  • After a completed ORF, scanning resumes at the index right after its stop codon, so completed ORFs never overlap.
  • If the frame runs out (fewer than 3 nucleotides left) before a stop codon, the ORF is discarded and scanning resumes at i + 1 . Nucleotides of a discarded frame can still belong to a later ORF.
  • Return an empty list when there is no complete ORF.

The standard codon table, grouped by the amino acid letter each codon produces:

F: TTT TTC                  L: TTA TTG CTT CTC CTA CTG
I: ATT ATC ATA              M: ATG
V: GTT GTC GTA GTG          S: TCT TCC TCA TCG AGT AGC
P: CCT CCC CCA CCG          T: ACT ACC ACA ACG
A: GCT GCC GCA GCG          Y: TAT TAC
H: CAT CAC                  Q: CAA CAG
N: AAT AAC                  K: AAA AAG
D: GAT GAC                  E: GAA GAG
C: TGT TGC                  W: TGG
R: CGT CGC CGA CGG AGA AGG  G: GGT GGC GGA GGG
Stop: TAA TAG TGA

Constraints

  • 1 <= len(sequence) <= 10**5
  • sequence consists only of the characters A , T , C , G .
  • Only complete ORFs (with both a start and a stop codon) are included in the result.

Examples

Input: sequence = "ATGAAATTTCCCTAGATGTGGTAAGGG"
Output: ["MKFP", "MW"]

The first ORF reads ATG, AAA, TTT, CCC and stops at TAG, giving MKFP, and scanning resumes at index 15. The second reads ATG, TGG and stops at TAA, giving MW, and the trailing GGG has no start codon.

Input: sequence = "AAATGATGCCCTAGTTT"
Output: ["MMP"]

The first ATG starts at index 2, and its frame reads ATG, ATG, CCC and then the stop TAG, so the protein is MMP. The TGA at index 3 overlaps the start codon in another frame, so it does not end the ORF, and the trailing TTT has no start codon.

Input: sequence = "ATGCATGGCCTAG"
Output: ["MA"]

The ATG at index 0 reads ATG, CAT, GGC, CTA and runs out of sequence before any stop, so it is discarded and scanning resumes at index 1. The ATG at index 4 reads ATG, GCC and then the stop TAG, which gives MA.