Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the input string is empty.
DSA Go
if len(s) == [1] { return true }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 to check for empty string.
Comparing len(s) to -1 which is invalid.
✗ Incorrect
An empty string has length 0, so we check if len(s) == 0.
2fill in blank
mediumComplete the code to iterate over the string indices for substring checks.
DSA Go
for i := 1; i <= [1]; i++ { // check substring }
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(s) - 1 which misses the last substring.
Using len(s) + 1 which causes out of range error.
✗ Incorrect
We iterate from 1 to len(s) inclusive to cover all substring lengths.
3fill in blank
hardFix the error in the substring extraction to get s[0:i].
DSA Go
prefix := s[[1]:i]Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 as start index which skips the first character.
Using i as start index which results in empty or wrong substring.
✗ Incorrect
To get the prefix substring from start to i, slice from 0 to i.
4fill in blank
hardFill both blanks to check if prefix is in wordDict and suffix can be segmented.
DSA Go
if wordDict[[1]] && dp[[2]] { dp[i] = true }
Attempts:
3 left
💡 Hint
Common Mistakes
Using suffix instead of prefix for dictionary check.
Using i instead of i - len(prefix) for dp index.
✗ Incorrect
We check if prefix is in wordDict and dp at index i - len(prefix) is true.
5fill in blank
hardFill all three blanks to complete the word break function using dynamic programming.
DSA Go
func wordBreak(s string, wordDict map[string]bool) bool {
dp := make([]bool, [1]+1)
dp[0] = true
for i := 1; i <= [2]; i++ {
for j := 0; j < i; j++ {
if dp[j] && wordDict[s[j:i]] {
dp[i] = true
break
}
}
}
return dp[[3]]
}Attempts:
3 left
💡 Hint
Common Mistakes
Using len(s) - 1 causing incomplete checks.
Returning dp[0] which only represents empty string.
✗ Incorrect
dp array size and loop limits use len(s), and we return dp[len(s)] for full string segmentation.
