educative.io

Regarding current solution of repeaated DNA sequence

Hi There,
The current Golang implementation for the solution looks tricky it should be straightforward (I don’t want to offend anyone :slight_smile: just feedback). I was hoping for a bit more clear, pragmatic, and concise code.

Something like below ()

package main

func findRepeatedSequences(s string, k int) Set {
	output := *NewSet()
	var m = map[string]struct{}{}
	var sub []byte
	for i := 0; i < k; i++{
		sub = append(sub, s[i])
	}
	m[string(sub)] = struct{}{}
	

	// fmt.Println(string(sub))

	for index := k; index <len(s); index++{
		sub = append(sub[1:], s[index])
		var currentSubStr = string(sub)

		if _, ok := m[currentSubStr]; ok{
			output.Add(currentSubStr)
			continue
		}
		m[currentSubStr] = struct{}{}
	}
	return output
}

2 Likes

Hi @HARVEER_SINGH , Thanks for reaching out to us.

We really appreciate that you have shared your thoughts with us and explained them very well. It is great seeing that you guys are always coming up with betterments. Your feedback is much appreciated. :blush:

1 Like

Hello @Rehan_Butt,

Although the code provided by @HARVEER_SINGH looks clean and is straightforward but after critically analyzing the code that you’ve shared, I found that it will not work correctly on a few test cases. For example, if we have a string “TTTT”, or “AAA” (a string containing the same character), with k always greater than the string length.

input string = “TTTT”
k = 7

The provided solution code throws an error saying “Error occurred while processing: runtime error: index out of range [4] with length 4.” To fix this, you will need to add a check at the beginning of the code:

n := len(s)

if n < k {
	return *NewSet()
}

The significance of these lines is to handle cases where the input string is too short to form substrings of the desired length (k). In such a scenario, finding repeated sequences is not possible, so the function returns an empty set as no repeated sequences can be found. This prevents further execution of the function for cases where the input is insufficient for the specified substring length.

@HARVEER_SINGH I hope this will help in understanding how your solution code acts in certain circumstances, also let me know if you need more details or comparison between the solution code provided in the lesson and your solution code.

As @Rehan_Butt we really appreciate when learners share their feedback on our courses and likewise, we love to help you out for better user experience. Feel free to share more suggestions and feedbacks. Thanks!

Happy learning!

1 Like

Thanks @Dian_Us_Suqlain for taking time and providing valuable feedback, would be great to have these corner test cases part of question test cases.
My suggestion was more about golang coding practices :slight_smile:

1 Like