Bird
Raised Fist0
DSA Goprogramming~20 mins

Word Break Problem in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Word Break Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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))
}
Afalse
Btrue
CCompilation error
DRuntime panic
Attempts:
2 left
💡 Hint
Check if the string "leetcode" can be split into words from the dictionary ["leet", "code"].
❓ Predict Output
intermediate
2:00remaining
Result 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))
}
Atrue
Bfalse
CCompilation error
DRuntime panic
Attempts:
2 left
💡 Hint
Check if "applepenapple" can be segmented using the dictionary words.
❓ Predict Output
advanced
2:00remaining
Output 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))
}
Afalse
Btrue
CCompilation error
DRuntime panic
Attempts:
2 left
💡 Hint
Check if "catsandog" can be segmented using the dictionary words.
🔧 Debug
advanced
2:00remaining
Identify 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))
}
AReturns false incorrectly
BCompilation error due to invalid slice
CIndex out of range runtime panic
DReturns true correctly
Attempts:
2 left
💡 Hint
Check the size of dp and the indices used to access it.
🧠 Conceptual
expert
2:00remaining
Time 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?
AO(n^3) because for each substring we check membership in the dictionary
BO(n^2 * k) where k is the average length of dictionary words due to substring checks
CO(m * n) because each dictionary word is checked against the string
DO(n^2) assuming dictionary lookups are O(1) with a hash set
Attempts:
2 left
💡 Hint
Consider the nested loops and dictionary lookup time.

Practice

(1/5)
1. What does the Word Break Problem ask you to determine about a given string s and a dictionary of words wordDict?
easy
A. If s can be segmented into a space-separated sequence of one or more dictionary words
B. If s contains any substring from wordDict
C. If s is exactly equal to any word in wordDict
D. If s can be reversed to form a word in wordDict

Solution

  1. Step 1: Understand the problem statement

    The Word Break Problem asks if the entire string s can be split into valid words from the dictionary without leftover characters.
  2. Step 2: Differentiate from similar concepts

    It is not enough for s to contain a substring or be equal to a dictionary word; the whole string must be fully segmented.
  3. Final Answer:

    If s can be segmented into a sequence of dictionary words -> Option A
  4. Quick Check:

    Word Break = Full segmentation check [OK]
Hint: Check if full string splits into dictionary words [OK]
Common Mistakes:
  • Thinking partial substring match is enough
  • Confusing equality with segmentation
  • Ignoring leftover characters after segmentation
2. Which of the following Go function signatures correctly represents a function to solve the Word Break Problem?
easy
A. func wordBreak(wordDict []string, s string) string
B. func wordBreak(s string, wordDict []string) bool
C. func wordBreak(s []string, wordDict string) int
D. func wordBreak(s string, wordDict map[string]int) []string

Solution

  1. Step 1: Identify correct parameter types

    The input string s should be a string, and the dictionary wordDict should be a slice of strings.
  2. Step 2: Identify correct return type

    The function returns a boolean indicating if segmentation is possible.
  3. Final Answer:

    func wordBreak(s string, wordDict []string) bool -> Option B
  4. Quick Check:

    Input: string + []string, Output: bool [OK]
Hint: Input string and word list, output bool [OK]
Common Mistakes:
  • Swapping parameter types
  • Returning wrong data type
  • Using map instead of slice without reason
3. What is the output of the following Go code snippet?
func main() {
    s := "leetcode"
    wordDict := []string{"leet", "code"}
    fmt.Println(wordBreak(s, wordDict))
}

func wordBreak(s string, wordDict []string) bool {
    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] && contains(wordDict, s[j:i]) {
                dp[i] = true
                break
            }
        }
    }
    return dp[len(s)]
}

func contains(dict []string, word string) bool {
    for _, w := range dict {
        if w == word {
            return true
        }
    }
    return false
}
medium
A. true
B. false
C. Compilation error
D. Runtime panic

Solution

  1. Step 1: Understand the dynamic programming approach

    The code uses a boolean slice dp where dp[i] is true if s[:i] can be segmented into dictionary words.
  2. Step 2: Trace the example string "leetcode"

    "leet" and "code" are in the dictionary, so dp[len(s)] becomes true.
  3. Final Answer:

    true -> Option A
  4. Quick Check:

    DP segmentation = true [OK]
Hint: Check dp array for full string length [OK]
Common Mistakes:
  • Forgetting to break inner loop after true
  • Incorrect substring slicing
  • Not initializing dp[0] to true
4. Identify the bug in this Go code for the Word Break Problem:
func wordBreak(s string, wordDict []string) bool {
    dp := make([]bool, len(s))
    dp[0] = true
    for i := 1; i <= len(s); i++ {
        for j := 0; j < i; j++ {
            if dp[j] && contains(wordDict, s[j:i]) {
                dp[i] = true
                break
            }
        }
    }
    return dp[len(s)]
}
medium
A. contains function is missing
B. dp slice length is correct but dp[0] should be false
C. dp slice length should be len(s)+1 and dp[0] initialization is correct
D. Loop indices i and j are reversed

Solution

  1. Step 1: Check dp slice size and initialization

    The dp slice must be of size len(s)+1 to include empty substring at index 0, and dp[0] should be true.
  2. Step 2: Verify loop bounds and dp usage

    Loops use indices up to len(s), so dp must have length len(s)+1 to avoid index out of range.
  3. Final Answer:

    dp slice length should be len(s)+1 and dp[0] initialization is correct -> Option C
  4. Quick Check:

    dp size = len(s)+1 [OK]
Hint: dp length = string length + 1 [OK]
Common Mistakes:
  • Using dp length as len(s) causes index errors
  • Not initializing dp[0] to true
  • Incorrect loop ranges causing panic
5. Given the string s = "applepenapple" and dictionary wordDict = ["apple", "pen"], which of the following modifications to the Word Break algorithm will correctly handle repeated words without extra space complexity?
hard
A. Replace dp array with a single boolean variable updated each iteration
B. Use a queue to store all possible segmentations and check each fully
C. Sort the dictionary and use binary search for substring checks
D. Use a memoization map to store results of substring checks to avoid recomputation

Solution

  1. Step 1: Understand repeated word handling

    Repeated words cause repeated substring checks; memoization stores results to avoid recomputation.
  2. Step 2: Analyze options for efficiency

    Memoization reduces time without increasing space beyond needed; queue or sorting do not directly optimize repeated checks; single boolean loses needed state.
  3. Final Answer:

    Use a memoization map to store results of substring checks to avoid recomputation -> Option D
  4. Quick Check:

    Memoization = efficient repeated checks [OK]
Hint: Memoize substring results to avoid repeated work [OK]
Common Mistakes:
  • Ignoring repeated substring computations
  • Using single boolean loses segmentation info
  • Assuming sorting dictionary helps substring checks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard