Skip to content

Practice DNA Pattern Recognition

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3475
LeetCode Problem

DNA Pattern Recognition

Problem Statement

Table Samples has sample_id INT, dna_sequence VARCHAR, and species VARCHAR. Return every sample with four integer flags: has_start for a sequence beginning ATG; has_stop for one ending TAA, TAG, or TGA; has_atat for an ATAT substring; and has_ggg for a run of at least three G characters. Preserve the original three columns and sort by sample_id ascending. Practice approach: Use independent prefix, suffix, and substring tests, converting each result to 1 or 0. Do not filter out samples without matches or require all patterns together. A longer G run also contains GGG.

Source: DNA Pattern Recognition on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
{
  "Samples": [
    {
      "sample_id": 1,
      "dna_sequence": "ATGGGGTAA",
      "species": "Bird"
    },
    {
      "sample_id": 2,
      "dna_sequence": "ATATTAG",
      "species": "Fish"
    },
    {
      "sample_id": 3,
      "dna_sequence": "CCATGCC",
      "species": "Bird"
    }
  ]
}
Output
[
  {
    "sample_id": 1,
    "dna_sequence": "ATGGGGTAA",
    "species": "Bird",
    "has_start": 1,
    "has_stop": 1,
    "has_atat": 0,
    "has_ggg": 1
  },
  {
    "sample_id": 2,
    "dna_sequence": "ATATTAG",
    "species": "Fish",
    "has_start": 0,
    "has_stop": 1,
    "has_atat": 1,
    "has_ggg": 0
  },
  {
    "sample_id": 3,
    "dna_sequence": "CCATGCC",
    "species": "Bird",
    "has_start": 0,
    "has_stop": 0,
    "has_atat": 0,
    "has_ggg": 0
  }
]
Explanation

The ATG inside the third sequence is not a start codon at its beginning, and the row remains in the result with zero flags.

Constraints

sample_id is unique.
Sequences use A, T, G, and C.

Related LeetCode Problems

Practice more problems similar to DNA Pattern Recognition .

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.