Exercise: DNA Sequence Slicer

Extract specific genetic markers using high-performance spans and string builders.

Problem statement

Bioinformatics applications process massive strings of genomic data where memory efficiency is critical. You must locate a specific genetic marker within a large DNA sequence, extract the subsequent 10 characters as the target gene, and format a final report.

Task requirements

  • Accept a full DNA string and a target marker string.

  • Find the exact starting index of the target marker.

  • Extract exactly 10 characters immediately following the marker.

  • Generate and return a report string formatted exactly as: "Analysis Complete. Target Sequence: [Sequence]".

  • Return "Incomplete target sequence" if there are fewer than 10 characters after the marker.

  • Return "Marker not found" if the sequence does not contain the target.

Constraints

  • Convert the input string to a ReadOnlySpan<char> using AsSpan() to avoid initial memory allocations.

  • Use the IndexOf() method on the span to locate the marker.

  • Use the Slice() method to extract the target sequence.

  • Use a StringBuilder to construct the final output string.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • You need to convert both the full sequence and the marker into spans to use the span-based IndexOf method.

  • The starting index of your slice is the found index of the marker plus the length of the marker itself.

  • Compare the required target slice length (10 characters) against the remaining length of the span before calling Slice() to prevent runtime out-of-bounds errors.

  • Modern .NET allows you to pass a ReadOnlySpan<char> directly into the StringBuilder.Append() method.

Exercise: DNA Sequence Slicer

Extract specific genetic markers using high-performance spans and string builders.

Problem statement

Bioinformatics applications process massive strings of genomic data where memory efficiency is critical. You must locate a specific genetic marker within a large DNA sequence, extract the subsequent 10 characters as the target gene, and format a final report.

Task requirements

  • Accept a full DNA string and a target marker string.

  • Find the exact starting index of the target marker.

  • Extract exactly 10 characters immediately following the marker.

  • Generate and return a report string formatted exactly as: "Analysis Complete. Target Sequence: [Sequence]".

  • Return "Incomplete target sequence" if there are fewer than 10 characters after the marker.

  • Return "Marker not found" if the sequence does not contain the target.

Constraints

  • Convert the input string to a ReadOnlySpan<char> using AsSpan() to avoid initial memory allocations.

  • Use the IndexOf() method on the span to locate the marker.

  • Use the Slice() method to extract the target sequence.

  • Use a StringBuilder to construct the final output string.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • You need to convert both the full sequence and the marker into spans to use the span-based IndexOf method.

  • The starting index of your slice is the found index of the marker plus the length of the marker itself.

  • Compare the required target slice length (10 characters) against the remaining length of the span before calling Slice() to prevent runtime out-of-bounds errors.

  • Modern .NET allows you to pass a ReadOnlySpan<char> directly into the StringBuilder.Append() method.

C# 14.0
using System.Text;
public class DnaAnalyzer
{
public string ProcessSequence(string dnaSequence, string marker)
{
// Write your high-performance slicing logic here
return string.Empty;
}
}