Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the dp table with zeros.
DSA Typescript
const n = dims.length - 1; const dp: number[][] = Array(n).fill(0).map(() => Array(n).fill([1]));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing dp with 1 instead of 0
Using Infinity which is used for initial max cost, not for dp initialization
✗ Incorrect
The dp table should be initialized with zeros because initially, the cost of multiplying one matrix is zero.
2fill in blank
mediumComplete the code to set the chain length for the outer loop.
DSA Typescript
for (let length = 2; length <= n; length[1]) {
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -- which decreases the length
Using *= which multiplies length
✗ Incorrect
The chain length increases by 1 in each iteration, so we use ++ to increment length.
3fill in blank
hardFix the error in the cost calculation inside the nested loops.
DSA Typescript
const cost = dp[i][k] + dp[k + 1][j] + dims[i] * dims[k + 1] * dims[[1] + 1];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using dims[k] instead of dims[j + 1]
Using dims[i] twice
✗ Incorrect
The cost of multiplying matrices from i to j includes dims[i] * dims[k+1] * dims[j + 1].
4fill in blank
hardFill both blanks to correctly update the dp table and loop indices.
DSA Typescript
for (let i = 0; i <= n - [1]; i++) { let j = i + [2] - 1;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using n instead of length
Incorrect calculation of j
✗ Incorrect
The loop runs while i <= n - length, and j is calculated as i + length - 1 to cover the chain segment.
5fill in blank
hardFill all three blanks to complete the minimum cost update inside the innermost loop.
DSA Typescript
if (cost < dp[i][j]) { dp[i][j] = [1]; split[i][j] = [2]; } // Return the minimum cost return dp[3];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning dp[i][j] to dp[i][j] instead of cost
Returning dp[i][j] instead of dp[0][n-1]
✗ Incorrect
We update dp[i][j] with cost, record split point k, and finally return dp[0][n-1] for full chain cost.