Challenge - 5 Problems
Word Break Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Word Break Check
What is the output of the following Go code that checks if the string can be segmented into dictionary words?
DSA Go
package main import "fmt" func wordBreak(s string, wordDict []string) bool { wordSet := make(map[string]bool) for _, w := range wordDict { wordSet[w] = true } dp := make([]bool, len(s)+1) dp[0] = true for i := 1; i <= len(s); i++ { for j := 0; j < i; j++ { if dp[j] && wordSet[s[j:i]] { dp[i] = true break } } } return dp[len(s)] } func main() { s := "leetcode" dict := []string{"leet", "code"} fmt.Println(wordBreak(s, dict)) }
Attempts:
2 left
💡 Hint
Check if the string "leetcode" can be split into words from the dictionary ["leet", "code"].
✗ Incorrect
The string "leetcode" can be split as "leet" + "code", both present in the dictionary, so the function returns true.
❓ Predict Output
intermediateResult of Word Break with No Valid Segmentation
What does the following Go code print when the string cannot be segmented into dictionary words?
DSA Go
package main import "fmt" func wordBreak(s string, wordDict []string) bool { wordSet := make(map[string]bool) for _, w := range wordDict { wordSet[w] = true } dp := make([]bool, len(s)+1) dp[0] = true for i := 1; i <= len(s); i++ { for j := 0; j < i; j++ { if dp[j] && wordSet[s[j:i]] { dp[i] = true break } } } return dp[len(s)] } func main() { s := "applepenapple" dict := []string{"apple", "pen", "pine"} fmt.Println(wordBreak(s, dict)) }
Attempts:
2 left
💡 Hint
Check if "applepenapple" can be segmented using the dictionary words.
✗ Incorrect
The string "applepenapple" can be segmented as "apple" + "pen" + "apple", so the output is true.
❓ Predict Output
advancedOutput of Word Break with Overlapping Words
What is the output of this Go program that tests word break with overlapping dictionary words?
DSA Go
package main import "fmt" func wordBreak(s string, wordDict []string) bool { wordSet := make(map[string]bool) for _, w := range wordDict { wordSet[w] = true } dp := make([]bool, len(s)+1) dp[0] = true for i := 1; i <= len(s); i++ { for j := 0; j < i; j++ { if dp[j] && wordSet[s[j:i]] { dp[i] = true break } } } return dp[len(s)] } func main() { s := "catsandog" dict := []string{"cats", "dog", "sand", "and", "cat"} fmt.Println(wordBreak(s, dict)) }
Attempts:
2 left
💡 Hint
Check if "catsandog" can be segmented using the dictionary words.
✗ Incorrect
The string "catsandog" cannot be segmented fully using the dictionary, so the output is false.
🔧 Debug
advancedIdentify the Bug in Word Break Implementation
What error will this Go code produce when run, and why?
DSA Go
package main import "fmt" func wordBreak(s string, wordDict []string) bool { wordSet := make(map[string]bool) for _, w := range wordDict { wordSet[w] = true } dp := make([]bool, len(s)) dp[0] = true for i := 1; i <= len(s); i++ { for j := 0; j < i; j++ { if dp[j] && wordSet[s[j:i]] { dp[i] = true break } } } return dp[len(s)-1] } func main() { s := "leetcode" dict := []string{"leet", "code"} fmt.Println(wordBreak(s, dict)) }
Attempts:
2 left
💡 Hint
Check the size of dp and the indices used to access it.
✗ Incorrect
dp slice is of length len(s), but code accesses dp[len(s)] causing index out of range panic.
🧠 Conceptual
expertTime Complexity of Word Break Algorithm
What is the time complexity of the dynamic programming solution for the Word Break problem where n is the length of the string and m is the number of words in the dictionary?
Attempts:
2 left
💡 Hint
Consider the nested loops and dictionary lookup time.
✗ Incorrect
The DP uses two nested loops over the string length (O(n^2)) and dictionary lookups are O(1) using a hash set, so total is O(n^2).
