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 output of the following Rust code that calculates the minimum multiplication cost for a given matrix chain?
DSA Rust
fn matrix_chain_order(p: &[usize]) -> usize {
let n = p.len() - 1;
let mut m = vec![vec![0usize; n]; n];
for l in 2..=n {
for i in 0..=n - l {
let j = i + l - 1;
m[i][j] = usize::MAX;
for k in i..j {
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;
}
}
}
}
m[0][n - 1]
}
fn main() {
let dims = vec![10usize, 20usize, 30usize, 40usize, 30usize];
println!("{}", matrix_chain_order(&dims));
}Attempts:
2 left
💡 Hint
Think about the optimal way to parenthesize the matrices to minimize multiplications.
✗ Incorrect
The minimum number of scalar multiplications needed to multiply matrices with dimensions 10x20, 20x30, 30x40, and 40x30 is 30000.
The code uses dynamic programming to find this minimum cost.
🧠 Conceptual
intermediateUnderstanding Matrix Chain Multiplication Dimensions
Given matrices A (5x10), B (10x3), C (3x12), what is the dimension of the resulting matrix after multiplying A * B * C?
Attempts:
2 left
💡 Hint
Matrix multiplication results in rows of the first and columns of the last matrix.
✗ Incorrect
Multiplying A (5x10) and B (10x3) results in a 5x3 matrix.
Then multiplying that result (5x3) with C (3x12) results in a 5x12 matrix.
🔧 Debug
advancedIdentify the Bug in Matrix Chain Multiplication Implementation
What error will the following Rust code produce when run?
DSA Rust
fn matrix_chain_order(p: &[usize]) -> usize {
let n = p.len();
let mut m = vec![vec![0usize; n]; n];
for l in 2..=n {
for i in 0..=n - l {
let j = i + l - 1;
m[i][j] = usize::MAX;
for k in i..j {
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;
}
}
}
}
m[0][n - 1]
}
fn main() {
let dims = vec![10usize, 20usize, 30usize, 40usize, 30usize];
println!("{}", matrix_chain_order(&dims));
}Attempts:
2 left
💡 Hint
Check how the length n is calculated and used for indexing.
✗ Incorrect
The code sets n = p.len(), but p has length 5 for 4 matrices.
Accessing p[j + 1] when j = n - 1 causes an index out of bounds panic.
🚀 Application
advancedMinimum Multiplication Cost for Given Dimensions
What is the minimum number of scalar multiplications needed to multiply matrices with dimensions [40, 20, 30, 10, 30]?
Attempts:
2 left
💡 Hint
Use dynamic programming or try different parenthesizations to find the minimum cost.
✗ Incorrect
The optimal parenthesization results in 26000 scalar multiplications.
🧠 Conceptual
expertNumber of Ways to Parenthesize Matrix Chain
How many ways are there to fully parenthesize a chain of 5 matrices for multiplication?
Attempts:
2 left
💡 Hint
The number of ways corresponds to the Catalan number for n-1 matrices.
✗ Incorrect
The number of ways to parenthesize n matrices is the (n-1)th Catalan number.
For 5 matrices, it's the 4th Catalan number = 14.
