JavaScript Coding Test Course, DNA Password

Coding tests are an important means of validating programming skills, and companies use them to assess the candidate’s algorithmic thinking and problem-solving abilities. In this course, we will closely examine the process of solving the DNA Password problem using JavaScript.

Problem Description

The DNA Password problem is as follows:

Problem: Given a DNA string of length N, find the number of all possible substrings from the DNA string that can be a password. The conditions for a password are that it must contain exactly 1 or 2 of the characters ‘A’, ‘C’, ‘G’, ‘T’, and must contain at least 1.

Example Input and Output


Input: "ACGTACGTA"
Output: 16

Problem Solving Process

To solve this problem, we will follow these steps:

Step 1: Problem Analysis

We need to find all substrings from the given DNA string that satisfy the password conditions. A password must contain either 1 or 2 of the characters ‘A’, ‘C’, ‘G’, ‘T’. Therefore, we need to consider cases for substrings that include each character.

Step 2: Generate Substrings

To generate all substrings of the DNA string, we can use two pointers to define the startIndex and endIndex. This process generates O(N^2) cases.

Step 3: Check Password Conditions

For each substring, we need to check the counts of ‘A’, ‘C’, ‘G’, ‘T’. We can use regular expressions or a counting array for this.

Step 4: Implement Code

Now, let’s implement the JavaScript code to solve the problem:


function countDNAPasswords(dna) {
    const n = dna.length;
    let count = 0;

    // Generate all substrings
    for (let start = 0; start < n; start++) {
        const charCount = { 'A': 0, 'C': 0, 'G': 0, 'T': 0 };
        
        for (let end = start; end < n; end++) {
            // Current character count
            const char = dna[end];
            if (charCount[char] !== undefined) {
                charCount[char]++;
            }

            // Check password conditions
            const uniqueCount = Object.values(charCount).filter(x => x > 0).length;
            if (uniqueCount >= 1 && uniqueCount <= 2) {
                count++;
            }
        }
    }

    return count;
}

// Example usage
const dnaString = "ACGTACGTA";
console.log(countDNAPasswords(dnaString)); // Output: 16

Code Explanation

The main functions of the code written above are as follows:

  • The countDNAPasswords function takes the DNA string as input and calculates the number of passwords.
  • Two nested for loops are used to generate all substrings.
  • For each substring, the charCount object is used to count the occurrences of ‘A’, ‘C’, ‘G’, ‘T’, and check the password conditions.
  • It counts the cases that meet the conditions.

Time Complexity Analysis

The time complexity of this algorithm is O(N^2). It generates all substrings using two nested loops. However, this method may lead to performance issues as the length of the string increases. Therefore, it is worth considering optimized methods or applying other algorithms.

Conclusion

In this course, we learned how to solve the DNA Password problem using JavaScript. We started from an algorithmic problem, implemented the code, and examined the process of deriving results in detail. Such problems frequently appear in coding tests, so consistent practice is necessary.

Through this practice, you can repeatedly improve your problem-solving skills, so it is recommended to tackle various problems. Thank you!