Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
Table: Samples
+----------------+---------+ | Column Name | Type | +----------------+---------+ | sample_id | int | | dna_sequence | varchar | | species | varchar | +----------------+---------+ sample_id is the unique key for this table. Each row contains a DNA sequence represented as a string of characters (A, T, G, C) and the species it was collected from.
Biologists are studying basic patterns in DNA sequences. Write a solution to identify sample_id with the following patterns:
3 consecutive G (like GGG or GGGG)Return the result table ordered by sample_id in ascending order.
The result format is in the following example.
Example:
Input:
Samples table:
+-----------+------------------+-----------+ | sample_id | dna_sequence | species | +-----------+------------------+-----------+ | 1 | ATGCTAGCTAGCTAA | Human | | 2 | GGGTCAATCATC | Human | | 3 | ATATATCGTAGCTA | Human | | 4 | ATGGGGTCATCATAA | Mouse | | 5 | TCAGTCAGTCAG | Mouse | | 6 | ATATCGCGCTAG | Zebrafish | | 7 | CGTATGCGTCGTA | Zebrafish | +-----------+------------------+-----------+
Output:
+-----------+------------------+-------------+-------------+------------+------------+------------+ | sample_id | dna_sequence | species | has_start | has_stop | has_atat | has_ggg | +-----------+------------------+-------------+-------------+------------+------------+------------+ | 1 | ATGCTAGCTAGCTAA | Human | 1 | 1 | 0 | 0 | | 2 | GGGTCAATCATC | Human | 0 | 0 | 0 | 1 | | 3 | ATATATCGTAGCTA | Human | 0 | 0 | 1 | 0 | | 4 | ATGGGGTCATCATAA | Mouse | 1 | 1 | 0 | 1 | | 5 | TCAGTCAGTCAG | Mouse | 0 | 0 | 0 | 0 | | 6 | ATATCGCGCTAG | Zebrafish | 0 | 1 | 1 | 0 | | 7 | CGTATGCGTCGTA | Zebrafish | 0 | 0 | 0 | 0 | +-----------+------------------+-------------+-------------+------------+------------+------------+
Explanation:
Note:
Problem summary: Table: Samples +----------------+---------+ | Column Name | Type | +----------------+---------+ | sample_id | int | | dna_sequence | varchar | | species | varchar | +----------------+---------+ sample_id is the unique key for this table. Each row contains a DNA sequence represented as a string of characters (A, T, G, C) and the species it was collected from. Biologists are studying basic patterns in DNA sequences. Write a solution to identify sample_id with the following patterns: Sequences that start with ATG (a common start codon) Sequences that end with either TAA, TAG, or TGA (stop codons) Sequences containing the motif ATAT (a simple repeated pattern) Sequences that have at least 3 consecutive G (like GGG or GGGG) Return the result table ordered by sample_id in ascending order. The result format is in the following example.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
{"headers":{"Samples":["sample_id","dna_sequence","species"]},"rows":{"Samples":[[1,"ATGCTAGCTAGCTAA","Human"],[2,"GGGTCAATCATC","Human"],[3,"ATATATCGTAGCTA","Human"],[4,"ATGGGGTCATCATAA","Mouse"],[5,"TCAGTCAGTCAG","Mouse"],[6,"ATATCGCGCTAG","Zebrafish"],[7,"CGTATGCGTCGTA","Zebrafish"]]}}Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3475: DNA Pattern Recognition
// Auto-generated Java example from py.
class Solution {
public void exampleSolution() {
}
}
// Reference (py):
// # Accepted solution for LeetCode #3475: DNA Pattern Recognition
// import pandas as pd
//
//
// def analyze_dna_patterns(samples: pd.DataFrame) -> pd.DataFrame:
// samples["has_start"] = samples["dna_sequence"].str.startswith("ATG").astype(int)
// samples["has_stop"] = (
// samples["dna_sequence"].str.endswith(("TAA", "TAG", "TGA")).astype(int)
// )
// samples["has_atat"] = samples["dna_sequence"].str.contains("ATAT").astype(int)
// samples["has_ggg"] = samples["dna_sequence"].str.contains("GGG+").astype(int)
// return samples.sort_values(by="sample_id").reset_index(drop=True)
// Accepted solution for LeetCode #3475: DNA Pattern Recognition
// Auto-generated Go example from py.
func exampleSolution() {
}
// Reference (py):
// # Accepted solution for LeetCode #3475: DNA Pattern Recognition
// import pandas as pd
//
//
// def analyze_dna_patterns(samples: pd.DataFrame) -> pd.DataFrame:
// samples["has_start"] = samples["dna_sequence"].str.startswith("ATG").astype(int)
// samples["has_stop"] = (
// samples["dna_sequence"].str.endswith(("TAA", "TAG", "TGA")).astype(int)
// )
// samples["has_atat"] = samples["dna_sequence"].str.contains("ATAT").astype(int)
// samples["has_ggg"] = samples["dna_sequence"].str.contains("GGG+").astype(int)
// return samples.sort_values(by="sample_id").reset_index(drop=True)
# Accepted solution for LeetCode #3475: DNA Pattern Recognition
import pandas as pd
def analyze_dna_patterns(samples: pd.DataFrame) -> pd.DataFrame:
samples["has_start"] = samples["dna_sequence"].str.startswith("ATG").astype(int)
samples["has_stop"] = (
samples["dna_sequence"].str.endswith(("TAA", "TAG", "TGA")).astype(int)
)
samples["has_atat"] = samples["dna_sequence"].str.contains("ATAT").astype(int)
samples["has_ggg"] = samples["dna_sequence"].str.contains("GGG+").astype(int)
return samples.sort_values(by="sample_id").reset_index(drop=True)
// Accepted solution for LeetCode #3475: DNA Pattern Recognition
// Rust example auto-generated from py reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (py):
// # Accepted solution for LeetCode #3475: DNA Pattern Recognition
// import pandas as pd
//
//
// def analyze_dna_patterns(samples: pd.DataFrame) -> pd.DataFrame:
// samples["has_start"] = samples["dna_sequence"].str.startswith("ATG").astype(int)
// samples["has_stop"] = (
// samples["dna_sequence"].str.endswith(("TAA", "TAG", "TGA")).astype(int)
// )
// samples["has_atat"] = samples["dna_sequence"].str.contains("ATAT").astype(int)
// samples["has_ggg"] = samples["dna_sequence"].str.contains("GGG+").astype(int)
// return samples.sort_values(by="sample_id").reset_index(drop=True)
// Accepted solution for LeetCode #3475: DNA Pattern Recognition
// Auto-generated TypeScript example from py.
function exampleSolution(): void {
}
// Reference (py):
// # Accepted solution for LeetCode #3475: DNA Pattern Recognition
// import pandas as pd
//
//
// def analyze_dna_patterns(samples: pd.DataFrame) -> pd.DataFrame:
// samples["has_start"] = samples["dna_sequence"].str.startswith("ATG").astype(int)
// samples["has_stop"] = (
// samples["dna_sequence"].str.endswith(("TAA", "TAG", "TGA")).astype(int)
// )
// samples["has_atat"] = samples["dna_sequence"].str.contains("ATAT").astype(int)
// samples["has_ggg"] = samples["dna_sequence"].str.contains("GGG+").astype(int)
// return samples.sort_values(by="sample_id").reset_index(drop=True)
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.