Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Matrix Chain Multiplication in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 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, 40] using the Matrix Chain Multiplication algorithm?
DSA Javascript
function matrixChainOrder(p) {
  const n = p.length - 1;
  const m = 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] = Infinity;
      for (let k = i; k < j; k++) {
        let 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, 40]));
A18000
B30000
C24000
D36000
Attempts:
2 left
💡 Hint
Think about the cost of multiplying matrices in different orders and choose the minimum.
🧠 Conceptual
intermediate
1:30remaining
Understanding Matrix Chain Multiplication Dimensions
Given matrices A (5x10), B (10x3), C (3x12), and D (12x5), what is the dimension of the resulting matrix after multiplying A * B * C * D?
A5 x 5
B5 x 12
C10 x 5
D12 x 5
Attempts:
2 left
💡 Hint
The resulting matrix dimension is the first matrix's row count and the last matrix's column count.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Matrix Chain Multiplication Implementation
What error will the following code produce when executed?
DSA Javascript
function matrixChainOrder(p) {
  const n = p.length - 1;
  const m = Array(n).fill(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] = Infinity;
      for (let k = i; k < j; k++) {
        let 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, 40]));
AOutput: 18000
BTypeError: Cannot assign to read only property '1' of object '[object Array]'
CReferenceError: m is not defined
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint
Check how the 2D array 'm' is initialized and whether each row is a separate array.
❓ Predict Output
advanced
2:30remaining
Output of Matrix Chain Multiplication Parenthesization
What is the optimal parenthesization for matrices with dimensions [40, 20, 30, 10, 30] that minimizes multiplication cost?
DSA Javascript
function printOptimalParens(s, i, j) {
  if (i === j) return `A${i + 1}`;
  return `(${printOptimalParens(s, i, s[i][j])} x ${printOptimalParens(s, s[i][j] + 1, j)})`;
}

function matrixChainOrder(p) {
  const n = p.length - 1;
  const m = Array.from({ length: n }, () => Array(n).fill(0));
  const s = 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] = Infinity;
      for (let k = i; k < j; k++) {
        let 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;
          s[i][j] = k;
        }
      }
    }
  }
  return printOptimalParens(s, 0, n - 1);
}

console.log(matrixChainOrder([40, 20, 30, 10, 30]));
A((A1 x (A2 x A3)) x A4)
B((A1 x A2) x (A3 x A4))
C(A1 x (A2 x (A3 x A4)))
D(((A1 x A2) x A3) x A4)
Attempts:
2 left
💡 Hint
The optimal parenthesization minimizes the total scalar multiplications by choosing the best split points.
🧠 Conceptual
expert
3:00remaining
Number of Scalar Multiplications for Large Chain
Given a chain of 5 matrices with dimensions [5, 10, 3, 12, 5, 50], what is the minimum number of scalar multiplications required to multiply the entire chain?
A2015
B2100
C2070
D2010
Attempts:
2 left
💡 Hint
Use dynamic programming to find the minimum cost by checking all splits.

Practice

(1/5)
1. What is the main goal of the Matrix Chain Multiplication problem?
easy
A. To count the total number of matrices in the chain
B. To multiply all matrices in the chain in the given order without optimization
C. To find the maximum number of matrices that can be multiplied
D. To find the minimum number of scalar multiplications needed to multiply a chain of matrices

Solution

  1. Step 1: Understand the problem objective

    The Matrix Chain Multiplication problem aims to multiply a sequence of matrices with minimum cost.
  2. Step 2: Identify what is minimized

    The cost minimized is the number of scalar multiplications, not just multiplying in order or counting matrices.
  3. Final Answer:

    To find the minimum number of scalar multiplications needed to multiply a chain of matrices -> Option D
  4. Quick Check:

    Goal = minimum scalar multiplications [OK]
Hint: Remember: minimize multiplication cost, not count matrices [OK]
Common Mistakes:
  • Thinking the problem is about counting matrices
  • Assuming multiplication order is fixed without optimization
  • Confusing maximum with minimum cost
2. Which of the following is the correct way to declare the dimensions array p for matrices A1 (10x30), A2 (30x5), A3 (5x60) in JavaScript?
easy
A. const p = [10, 30, 5, 60];
B. const p = [[10, 30], [30, 5], [5, 60]];
C. const p = [10, 30, 5];
D. const p = [30, 5, 60];

Solution

  1. Step 1: Understand the dimensions array format

    The array p stores dimensions such that matrix Ai has dimensions p[i-1] x p[i].
  2. Step 2: Apply to given matrices

    For A1 (10x30), A2 (30x5), A3 (5x60), p = [10, 30, 5, 60].
  3. Final Answer:

    const p = [10, 30, 5, 60]; -> Option A
  4. Quick Check:

    Dimensions array = [10,30,5,60] [OK]
Hint: Dimensions array length = number of matrices + 1 [OK]
Common Mistakes:
  • Using nested arrays instead of flat array
  • Missing last dimension in array
  • Confusing matrix dimensions order
3. What is the output of the following JavaScript code snippet for matrix dimensions p = [40, 20, 30, 10, 30]?
function matrixChainOrder(p) {
  const n = p.length - 1;
  const m = 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] = Infinity;
      for (let k = i; k < j; k++) {
        let 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(p));
medium
A. 45000
B. 30000
C. 26000
D. None of the above

Solution

  1. Step 1: Understand the code logic

    The code implements the dynamic programming solution for matrix chain multiplication to find minimum cost.
  2. Step 2: Calculate minimum multiplication cost for p = [40,20,30,10,30]

    Minimum cost is 26000 scalar multiplications after checking all splits.
  3. Final Answer:

    26000 -> Option C
  4. Quick Check:

    Minimum cost for given p = 26000 [OK]
Hint: Use DP table to find minimum cost, result is m[0][n-1] [OK]
Common Mistakes:
  • Confusing indices i, j, k in loops
  • Incorrectly calculating multiplication cost
  • Returning wrong DP table value
4. Identify the error in the following JavaScript code snippet for matrix chain multiplication and select the fix:
function matrixChainOrder(p) {
  const n = p.length - 1;
  const m = 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] = Infinity;
      for (let k = i; k < j; k++) {
        let 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];
}
medium
A. Initialize m with Infinity instead of 0
B. Change inner loop condition to i <= n - L
C. Use for (let k = i; k <= j; k++) instead of k < j
D. Return m[0][n] instead of m[0][n - 1]

Solution

  1. Step 1: Analyze the outer loop range for i

    The loop for (let i = 0; i < n - L; i++) misses the case when i equals n - L, causing incomplete computation.
  2. Step 2: Fix loop condition to include i = n - L

    Changing to i <= n - L ensures all subproblems are covered correctly.
  3. Final Answer:

    Change inner loop condition to i <= n - L -> Option B
  4. Quick Check:

    Loop range fix = i <= n - L [OK]
Hint: Check loop boundaries carefully for full coverage [OK]
Common Mistakes:
  • Off-by-one error in loop ranges
  • Incorrect DP table initialization
  • Wrong return index from DP table
5. Given matrices with dimensions p = [5, 10, 3, 12, 5, 50, 6], what is the minimum number of scalar multiplications needed to multiply the entire chain? Use the matrix chain multiplication approach.
hard
A. 1510
B. 2010
C. 4050
D. None of the above

Solution

  1. Step 1: Understand the problem and dimensions

    We have 6 matrices with dimensions from p array: A1(5x10), A2(10x3), A3(3x12), A4(12x5), A5(5x50), A6(50x6).
  2. Step 2: Apply matrix chain multiplication DP to find minimum cost

    Calculating minimum scalar multiplications yields 1510 as the optimal cost.
  3. Final Answer:

    1510 -> Option A
  4. Quick Check:

    Minimum scalar multiplications = 1510 [OK]
Hint: Use DP table and check all splits for minimum cost [OK]
Common Mistakes:
  • Calculating cost for wrong matrix pairs
  • Missing optimal parenthesization
  • Confusing dimensions order in multiplication
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard