0
0
DSA Typescriptprogramming~20 mins

Matrix Chain Multiplication in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Matrix Chain Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Matrix Chain Multiplication Cost Calculation
What is the minimum number of scalar multiplications needed to multiply the chain of matrices with dimensions [10, 20, 30] using the matrix chain multiplication algorithm?
DSA Typescript
function matrixChainOrder(p: number[]): number {
  const n = p.length - 1;
  const m: number[][] = Array.from({ length: n }, () => Array(n).fill(0));

  for (let L = 2; L <= n; L++) {
    for (let i = 0; i <= n - L; i++) {
      let j = i + L - 1;
      m[i][j] = Number.MAX_SAFE_INTEGER;
      for (let k = i; k < j; k++) {
        const q = m[i][k] + m[k + 1][j] + p[i] * p[k + 1] * p[j + 1];
        if (q < m[i][j]) m[i][j] = q;
      }
    }
  }
  return m[0][n - 1];
}

console.log(matrixChainOrder([10, 20, 30]));
A9000
B6000
C18000
D15000
Attempts:
2 left
💡 Hint
Think about the cost formula for multiplying two matrices and how the algorithm tries all splits.
🧠 Conceptual
intermediate
1:30remaining
Understanding the Role of the 'k' Variable in Matrix Chain Multiplication
In the matrix chain multiplication dynamic programming solution, what does the variable 'k' represent inside the nested loops?
A'k' represents the index where the matrix chain is split to compute the cost of multiplication.
B'k' is the total number of matrices in the chain.
C'k' is the dimension of the resulting matrix after multiplication.
D'k' is the minimum cost found so far for multiplying the matrices.
Attempts:
2 left
💡 Hint
Think about how the algorithm tries different places to split the chain.
🔧 Debug
advanced
2:00remaining
Identify the Error in Matrix Chain Multiplication Implementation
What error will the following TypeScript code produce when executed?
DSA Typescript
function matrixChainOrder(p: number[]): number {
  const n = p.length - 1;
  const m: number[][] = Array.from({ length: n }, () => Array(n).fill(0));

  for (let L = 2; L <= n; L++) {
    for (let i = 0; i <= n - L; i++) {
      let j = i + L - 1;
      m[i][j] = Number.MAX_SAFE_INTEGER;
      for (let k = i; k < j; k++) {
        const q = m[i][k] + m[k + 1][j] + p[i] * p[k + 1] * p[j + 1];
        if (q < m[i][j]) m[i][j] = q;
      }
    }
  }
  return m[0][n - 1];
}

console.log(matrixChainOrder([10, 20, 30]));
ATypeError: Cannot read property 'j+1' of undefined
BIndexError: Array index out of bounds
CThe function returns 0 instead of the correct minimum cost
DNo error, outputs 6000
Attempts:
2 left
💡 Hint
Check the loop boundaries carefully, especially the inner loop conditions.
Predict Output
advanced
2:30remaining
Output of Matrix Chain Multiplication with Multiple Matrices
What is the minimum number of scalar multiplications needed to multiply the chain of matrices with dimensions [40, 20, 30, 10, 30]?
DSA Typescript
function matrixChainOrder(p: number[]): number {
  const n = p.length - 1;
  const m: number[][] = Array.from({ length: n }, () => Array(n).fill(0));

  for (let L = 2; L <= n; L++) {
    for (let i = 0; i <= n - L; i++) {
      let j = i + L - 1;
      m[i][j] = Number.MAX_SAFE_INTEGER;
      for (let k = i; k < j; k++) {
        const q = m[i][k] + m[k + 1][j] + p[i] * p[k + 1] * p[j + 1];
        if (q < m[i][j]) m[i][j] = q;
      }
    }
  }
  return m[0][n - 1];
}

console.log(matrixChainOrder([40, 20, 30, 10, 30]));
A26000
B30000
C45000
D35000
Attempts:
2 left
💡 Hint
Try to find the optimal split points to minimize multiplications.
🚀 Application
expert
3:00remaining
Number of Optimal Parenthesizations for Matrix Chain Multiplication
Given the matrix dimensions [10, 20, 30, 40], how many different ways are there to parenthesize the matrix chain multiplication that achieve the minimum number of scalar multiplications?
A3
B2
C1
D4
Attempts:
2 left
💡 Hint
Consider how many ways the chain can be split to achieve the same minimum cost.