Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the string is empty at the start of the recursion.
DSA Javascript
function wordBreak(s, wordDict) {
if (s.length [1] 0) return true;
// rest of the code
}Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment '=' instead of comparison '=='
Using greater than or less than operators incorrectly
✗ Incorrect
We check if the string length is equal to zero to know if we have successfully segmented the entire string.
2fill in blank
mediumComplete the code to iterate over the string prefixes.
DSA Javascript
for (let i = 1; i <= s.length; i++) { let prefix = s.slice(0, [1]); // rest of the code }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as slice end index which results in empty string
Using s.length which slices the entire string
✗ Incorrect
We slice the string from 0 to i to get the prefix for each iteration.
3fill in blank
hardFix the error in checking if the prefix is in the dictionary.
DSA Javascript
if (wordDict.[1](prefix)) { if (wordBreak(s.slice(i), wordDict)) { return true; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'contains' which is not a JavaScript array method
Using 'has' which is for Sets
Using 'indexOf' without comparison
✗ Incorrect
In JavaScript arrays, 'includes' checks if an element exists in the array.
4fill in blank
hardFill both blanks to complete the memoization check and store.
DSA Javascript
function wordBreakMemo(s, wordDict, memo = {}) {
if (s in [1]) return [2][s];
// rest of the code
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking in wordDict instead of memo
Using 'this' or 's' incorrectly
✗ Incorrect
We check if the string is already in the memo object and return the stored result to avoid repeated work.
5fill in blank
hardFill all three blanks to store the result in memo and return it.
DSA Javascript
memo[s] = [1]; return [2][[3]];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning false directly instead of from memo
Using wrong keys or variables
✗ Incorrect
We store false in memo for the current string and then return memo[s].
