0
0
DSA Cprogramming~20 mins

Matrix Chain Multiplication in DSA C - Practice Problems & Challenges

Choose your learning style9 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 C
int arr[] = {10, 20, 30, 40};
int n = 4;
int matrixChainOrder(int p[], int n) {
    int m[n][n];
    for (int i = 1; i < n; i++)
        m[i][i] = 0;
    for (int L = 2; L < n; L++) {
        for (int i = 1; i < n - L + 1; i++) {
            int j = i + L - 1;
            m[i][j] = 1000000;
            for (int k = i; k <= j - 1; k++) {
                int q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
                if (q < m[i][j])
                    m[i][j] = q;
            }
        }
    }
    return m[1][n - 1];
}

int result = matrixChainOrder(arr, n);
printf("%d", result);
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), what is the dimension of the resulting matrix after multiplying A * B * C?
A5 x 3
B5 x 12
C10 x 12
D3 x 12
Attempts:
2 left
💡 Hint
The resulting matrix dimension after multiplication is the number of rows of the first matrix and the number of columns of the last matrix.
Predict Output
advanced
2:00remaining
Output of Matrix Chain Multiplication Parenthesization Count
What is the number of ways to fully parenthesize a chain of 4 matrices for multiplication?
DSA C
int countParenthesizations(int n) {
    if (n <= 1) return 1;
    int count = 0;
    for (int i = 1; i < n; i++) {
        count += countParenthesizations(i) * countParenthesizations(n - i);
    }
    return count;
}

int result = countParenthesizations(4);
printf("%d", result);
A5
B14
C4
D2
Attempts:
2 left
💡 Hint
The number of ways to parenthesize n matrices is the (n-1)th Catalan number.
🔧 Debug
advanced
2:00remaining
Identify the Error in Matrix Chain Multiplication Code
What error does the following code produce when run?
DSA C
int matrixChainOrder(int p[], int n) {
    int m[n][n];
    for (int i = 1; i < n; i++)
        m[i][i] = 0;
    for (int L = 2; L < n; L++) {
        for (int i = 1; i < n - L + 1; i++) {
            int j = i + L - 1;
            m[i][j] = 1000000;
            for (int k = i; k <= j - 1; k++) {
                int q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
                if (q < m[i][j])
                    m[i][j] = q;
            }
        }
    }
    return m[1][n - 1];
}
ANo error, runs correctly
BSyntax error due to missing semicolon
CArray index out of bounds error
DType error due to incompatible types
Attempts:
2 left
💡 Hint
Check the loop boundaries carefully for array indexing.
🚀 Application
expert
3:00remaining
Minimum Multiplication Cost for Large Matrix Chain
Given the matrix dimensions array arr = [40, 20, 30, 10, 30], what is the minimum number of scalar multiplications needed to multiply the chain of matrices?
DSA C
int arr[] = {40, 20, 30, 10, 30};
int n = 5;
int matrixChainOrder(int p[], int n) {
    int m[n][n];
    for (int i = 1; i < n; i++)
        m[i][i] = 0;
    for (int L = 2; L < n; L++) {
        for (int i = 1; i < n - L + 1; i++) {
            int j = i + L - 1;
            m[i][j] = 1000000;
            for (int k = i; k <= j - 1; k++) {
                int q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
                if (q < m[i][j])
                    m[i][j] = q;
            }
        }
    }
    return m[1][n - 1];
}

int result = matrixChainOrder(arr, n);
printf("%d", result);
A27000
B30000
C35000
D26000
Attempts:
2 left
💡 Hint
Try different parenthesizations and calculate the cost for each.