0
0
DSA Typescriptprogramming~10 mins

Matrix Chain Multiplication in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A1
B0
CInfinity
D-1
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
2fill in blank
medium

Complete 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'
A++
B*=
C+=
D--
Attempts:
3 left
💡 Hint
Common Mistakes
Using -- which decreases the length
Using *= which multiplies length
3fill in blank
hard

Fix 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'
Ai
Bk
Cj
Dk - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using dims[k] instead of dims[j + 1]
Using dims[i] twice
4fill in blank
hard

Fill 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'
Alength
Cn
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using n instead of length
Incorrect calculation of j
5fill in blank
hard

Fill 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'
Acost
Bk
C[0][n - 1]
Ddp[i][j]
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]