Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the input string is empty, which means successful segmentation.
DSA Python
def word_break(s, word_dict): if s == [1]: return True
Attempts:
3 left
💡 Hint
Common Mistakes
Using None instead of an empty string.
Using 0 which is a number, not a string.
✗ Incorrect
An empty string '' means we have segmented the entire input successfully.
2fill in blank
mediumComplete the code to iterate over the string prefixes to check if they are in the dictionary.
DSA Python
for i in range(1, [1] + 1): prefix = s[:i]
Attempts:
3 left
💡 Hint
Common Mistakes
Using the string itself instead of its length.
Using the dictionary length instead of string length.
✗ Incorrect
We use len(s) to get the length of the string to iterate over all prefixes.
3fill in blank
hardFix the error in the recursive call to check the suffix after the prefix is found in the dictionary.
DSA Python
if prefix in word_dict and word_break(s[1], word_dict): return True
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets for slicing.
Using s[i] which is a single character, not a substring.
✗ Incorrect
We slice the string from index i to the end using s[i:] to check the suffix.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps words to their lengths for quick lookup.
DSA Python
word_dict = { [1]: len([2]) for [1] in words }Attempts:
3 left
💡 Hint
Common Mistakes
Using 'words' instead of 'word' as the variable name.
Using 'len' as the variable name instead of the function.
✗ Incorrect
We use 'word' as the variable name for each element in words and map it to its length.
5fill in blank
hardFill all three blanks to complete the memoization dictionary usage in the word break function.
DSA Python
def word_break_memo(s, word_dict, memo): if s in memo: return [1] for i in range(1, len(s) + 1): prefix = s[:i] if prefix in word_dict and word_break_memo(s[2], word_dict, memo): memo[s] = [3] return True memo[s] = False return False
Attempts:
3 left
💡 Hint
Common Mistakes
Returning memo instead of memo[s].
Using parentheses instead of square brackets for slicing.
Storing False instead of True when segmentation succeeds.
✗ Incorrect
We return memo[s] if s is in memo, slice s[i:] for recursion, and store True when segmentation is possible.
