Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the string can be segmented by dictionary words.
DSA Typescript
function wordBreak(s: string, wordDict: string[]): boolean {
const dp: boolean[] = Array(s.length + 1).fill(false);
dp[0] = true;
for (let i = 1; i <= s.length; i++) {
for (let j = 0; j < i; j++) {
if (dp[j] && wordDict.includes(s.substring(j, [1]))) {
dp[i] = true;
break;
}
}
}
return dp[s.length];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using j instead of i causes checking empty or wrong substring.
Using s.length causes out of range substring.
✗ Incorrect
The substring end index should be 'i' to check the segment from j to i.
2fill in blank
mediumComplete the code to initialize the dp array correctly.
DSA Typescript
function wordBreak(s: string, wordDict: string[]): boolean {
const dp: boolean[] = Array([1]).fill(false);
dp[0] = true;
// rest of the code
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using s.length causes index out of range errors.
Using wordDict.length is unrelated to dp size.
✗ Incorrect
dp array size should be s.length + 1 to include empty substring at dp[0].
3fill in blank
hardFix the error in the loop condition to avoid infinite loops.
DSA Typescript
for (let i = 1; [1]; i++) { for (let j = 0; j < i; j++) { // code } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using i < s.length causes missing last substring checks.
Using i >= s.length or i > s.length causes infinite or no loops.
✗ Incorrect
The outer loop must run until i <= s.length to cover full string length.
4fill in blank
hardFill both blanks to correctly check substring and update dp array.
DSA Typescript
if (dp[[1]] && wordDict.includes(s.substring([2], i))) { dp[i] = true; break; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using i in dp or substring start causes wrong checks.
Using 0 or s.length causes incorrect substring ranges.
✗ Incorrect
Both blanks should be 'j' to check substring from j to i and dp[j].
5fill in blank
hardFill all three blanks to complete the word break function correctly.
DSA Typescript
function wordBreak(s: string, wordDict: string[]): boolean {
const dp: boolean[] = Array([1]).fill(false);
dp[0] = true;
for (let i = 1; i [2] s.length; i++) {
for (let j = 0; j < i; j++) {
if (dp[[3]] && wordDict.includes(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
return dp[s.length];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using dp size as s.length causes index errors.
Using i < s.length misses last character.
Using dp[i] instead of dp[j] causes logic errors.
✗ Incorrect
dp size is s.length+1, loop runs while i <= s.length, and dp[j] checks previous valid segment.