Challenge - 5 Problems
Matrix Chain Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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]));Attempts:
2 left
💡 Hint
Think about the cost of multiplying matrices in different orders and choose the minimum.
✗ Incorrect
The minimum cost is calculated by trying all possible splits and choosing the one with the least scalar multiplications. For matrices with dimensions 10x20, 20x30, and 30x40, the minimum cost is 18000.
🧠 Conceptual
intermediateUnderstanding 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?
Attempts:
2 left
💡 Hint
The resulting matrix dimension is the first matrix's row count and the last matrix's column count.
✗ Incorrect
When multiplying matrices, the resulting matrix has the number of rows of the first matrix and the number of columns of the last matrix. Here, A is 5x10 and D is 12x5, so the result is 5x5.
🔧 Debug
advancedIdentify 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]));Attempts:
2 left
💡 Hint
Check how the 2D array 'm' is initialized and whether each row is a separate array.
✗ Incorrect
The code uses Array(n).fill(Array(n).fill(0)) which fills the outer array with references to the same inner array. Modifying one row modifies all rows, causing assignment errors.
❓ Predict Output
advancedOutput 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]));Attempts:
2 left
💡 Hint
The optimal parenthesization minimizes the total scalar multiplications by choosing the best split points.
✗ Incorrect
The optimal order is to multiply A3 and A4 first, then multiply A2 with that result, and finally multiply A1 with the result, represented as (A1 x (A2 x (A3 x A4))).
🧠 Conceptual
expertNumber 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?
Attempts:
2 left
💡 Hint
Use dynamic programming to find the minimum cost by checking all splits.
✗ Incorrect
The minimum cost is found by trying all possible splits and choosing the minimum. For these dimensions, the minimum scalar multiplications is 2010.
