Challenge - 5 Problems
Matrix Chain Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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]));Attempts:
2 left
💡 Hint
Think about the cost formula for multiplying two matrices and how the algorithm tries all splits.
✗ Incorrect
The minimum cost is calculated by trying all possible splits. For matrices with dimensions 10x20 and 20x30, the cost is 10*20*30 = 6000 scalar multiplications.
🧠 Conceptual
intermediate1: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?
Attempts:
2 left
💡 Hint
Think about how the algorithm tries different places to split the chain.
✗ Incorrect
The variable 'k' is used to try all possible places to split the matrix chain between i and j to find the minimum multiplication cost.
🔧 Debug
advanced2: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]));Attempts:
2 left
💡 Hint
Check the loop boundaries carefully, especially the inner loop conditions.
✗ Incorrect
The original code had 'i < n - L' which caused skipping the last valid index. Changing it to 'i <= n - L' fixes the issue and the function outputs the correct minimum cost 6000.
❓ Predict Output
advanced2: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]));Attempts:
2 left
💡 Hint
Try to find the optimal split points to minimize multiplications.
✗ Incorrect
The minimum cost is 26000 scalar multiplications by splitting the chain optimally.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Consider how many ways the chain can be split to achieve the same minimum cost.
✗ Incorrect
There is exactly 1 way to parenthesize the chain [10,20,30,40] that yields the minimum cost of 18000: ((A1 A2) A3). The other parenthesization (A1 (A2 A3)) costs 32000.