Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if a substring is a palindrome.
DSA Go
func isPalindrome(s string, left int, right int) bool {
for left < right {
if s[left] != s[1] {
return false
}
left++
right--
}
return true
}Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or braces instead of square brackets for indexing.
Confusing the order of left and right indices.
✗ Incorrect
We use s[left] and s[right] to compare characters at positions left and right. The correct syntax for indexing a string in Go is s[index] with square brackets.
2fill in blank
mediumComplete the code to append a substring to the current partition.
DSA Go
func backtrack(s string, start int, path []string, result *[][]string) {
if start == len(s) {
temp := make([]string, len(path))
copy(temp, path)
*result = append(*result, temp)
return
}
for end := start; end < len(s); end++ {
if isPalindrome(s, start, end) {
path = append(path, s[1])
backtrack(s, end+1, path, result)
path = path[:len(path)-1]
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or braces instead of square brackets for slicing.
Not adding 1 to the end index, missing the last character.
✗ Incorrect
In Go, slicing a string uses square brackets with start and end indices: s[start:end]. To include the character at end, we use end+1.
3fill in blank
hardFix the error in the backtracking function call to correctly pass the result pointer.
DSA Go
func partition(s string) [][]string {
var result [][]string
backtrack(s, 0, []string{}, [1])
return result
}Attempts:
3 left
💡 Hint
Common Mistakes
Passing the slice by value, which prevents the function from modifying the original result.
Using *result which is invalid here because result is not a pointer.
✗ Incorrect
The backtrack function expects a pointer to the result slice, so we pass &result to allow modifications inside the function.
4fill in blank
hardFill both blanks to complete the palindrome partitioning backtracking loop.
DSA Go
for end := [1]; end < [2]; end++ { if isPalindrome(s, start, end) { path = append(path, s[start:end+1]) backtrack(s, end+1, path, result) path = path[:len(path)-1] } }
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop from 0 instead of start.
Using len(path) instead of len(s) for loop limit.
✗ Incorrect
The loop starts from 'start' index and goes until the length of the string 'len(s)' to check all possible substrings.
5fill in blank
hardFill all three blanks to complete the partition function with backtracking.
DSA Go
func partition(s string) [][]string {
var result [][]string
backtrack(s, [1], [2], [3])
return result
}Attempts:
3 left
💡 Hint
Common Mistakes
Passing nil instead of an empty slice for path.
Not passing the pointer to result.
Starting from an index other than 0.
✗ Incorrect
We start from index 0, use an empty slice for the current path, and pass the address of result to collect partitions.
