Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the DP table with infinity.
DSA Rust
let n = dims.len(); let mut dp = vec![vec![[1]; n]; n];
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing with 0 causes no updates since costs are positive and greater than 0.
Using 1 or -1 is incorrect for infinity.
✗ Incorrect
The DP table should be initialized with usize::MAX to represent infinity for uncomputed minimum costs. The diagonal will be overridden to 0.
2fill in blank
mediumComplete the code to set the initial cost of multiplying one matrix to zero.
DSA Rust
for i in 0..n { dp[i][i] = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[i][i] to 1 or -1 instead of 0.
Leaving it as usize::MAX would make single matrix cost infinity.
✗ Incorrect
The cost to multiply a single matrix is zero, so dp[i][i] should be set to 0.
3fill in blank
hardFix the error in the cost calculation inside the nested loops.
DSA Rust
let cost = dp[i][k] + dp[k + 1][j] + dims[i] * dims[k + 1] * [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Using dims[j] instead of dims[j+1] uses wrong dimension.
Using dims[k] or dims[i+1] causes wrong cost calculation.
✗ Incorrect
The cost formula is dp[i][k] + dp[k+1][j] + dims[i] * dims[k+1] * dims[j+1], where dims[j+1] is the number of columns of the last matrix in the chain.
4fill in blank
hardFill both blanks to correctly update the minimum cost in the DP table.
DSA Rust
if cost < dp[[1]][[2]] { dp[i][j] = cost; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using k or k+1 as indices instead of i and j.
Updating dp[k][j] or dp[i][k] instead of dp[i][j].
✗ Incorrect
We compare the new cost with dp[i][j] to update the minimum cost for multiplying matrices from i to j.
5fill in blank
hardFill all three blanks to complete the nested loops for the matrix chain multiplication algorithm.
DSA Rust
for length in 2..=[1] { for i in 0..=[2] - length { let j = i + length - [3]; // compute dp[i][j] } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using n instead of n-1 for loop limits.
Incorrect calculation of j index.
✗ Incorrect
The outer loop runs length from 2 to n-1, inner loop i runs from 0 to n-1-length, and j is calculated as i + length - 1.
