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] using the matrix chain multiplication algorithm?
DSA Go
package main import "fmt" func matrixChainOrder(p []int) int { n := len(p) - 1 m := make([][]int, n) for i := range m { m[i] = make([]int, n) } for l := 2; l <= n; l++ { for i := 0; i <= n-l; i++ { j := i + l - 1 m[i][j] = 1<<31 - 1 for k := i; k < j; k++ { 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] } func main() { dims := []int{10, 20, 30} fmt.Println(matrixChainOrder(dims)) }
Attempts:
2 left
💡 Hint
Think about the cost of multiplying two matrices of dimensions p x q and q x r.
✗ Incorrect
The minimum cost is calculated by trying all possible splits. For matrices 10x20 and 20x30, the cost is 10*20*30 = 6000 scalar multiplications.
🧠 Conceptual
intermediateUnderstanding the Role of Dimensions Array in Matrix Chain Multiplication
In the matrix chain multiplication problem, what does the array of dimensions represent?
Attempts:
2 left
💡 Hint
Each matrix has rows and columns; the array stores these values in sequence.
✗ Incorrect
The dimensions array has length n+1 for n matrices, where the ith matrix has dimensions p[i-1] x p[i].
❓ Predict Output
advancedOutput of Matrix Chain Multiplication with Multiple Matrices
What is the minimum number of scalar multiplications needed to multiply matrices with dimensions [40, 20, 30, 10, 30]?
DSA Go
package main import "fmt" func matrixChainOrder(p []int) int { n := len(p) - 1 m := make([][]int, n) for i := range m { m[i] = make([]int, n) } for l := 2; l <= n; l++ { for i := 0; i <= n-l; i++ { j := i + l - 1 m[i][j] = 1<<31 - 1 for k := i; k < j; k++ { 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] } func main() { dims := []int{40, 20, 30, 10, 30} fmt.Println(matrixChainOrder(dims)) }
Attempts:
2 left
💡 Hint
Try different ways to parenthesize the multiplication and calculate costs.
✗ Incorrect
The minimum cost is 26000 scalar multiplications by optimal parenthesization.
🔧 Debug
advancedIdentify the Error in Matrix Chain Multiplication Code
What error will this Go code produce when run?
func matrixChainOrder(p []int) int {
n := len(p) - 1
m := make([][]int, n)
for i := range m {
m[i] = make([]int, n)
}
for l := 2; l <= n; l++ {
for i := 0; i <= n-l; i++ {
j := i + l - 1
m[i][j] = 1<<31
for k := i; k < j; k++ {
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]
}
Attempts:
2 left
💡 Hint
Check the expression used to initialize m[i][j].
✗ Incorrect
1<<31 is an invalid shift for int on some systems; it should be 1<<31 - 1 to represent max int.
🚀 Application
expertNumber of Ways to Parenthesize Matrix Chain Multiplication
Given 5 matrices, how many different ways are there to fully parenthesize the product (i.e., different valid multiplication orders)?
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 C4 = 14.
