Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the dp table with zeros.
DSA C#
int n = dims.Length - 1; int[,] dp = new int[n, n]; for (int i = 0; i < n; i++) { dp[i, i] = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[i, i] to 1 or -1 instead of 0.
Using int.MaxValue which is for initialization of minimum cost comparisons.
✗ Incorrect
The diagonal elements represent single matrices, so their multiplication cost is zero.
2fill in blank
mediumComplete the code to calculate the cost of multiplying two matrices.
DSA C#
int cost = dp[i, k] + dp[k + 1, j] + dims[i] * dims[k + 1] * [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Using dims[j] incorrectly in the formula.
Using dims[k] or dims[i] incorrectly in the formula.
✗ Incorrect
The cost formula uses dims[i] * dims[k+1] * dims[j+1] for matrix multiplication cost.
3fill in blank
hardFix the error in the loop condition to correctly iterate over chain lengths.
DSA C#
for (int length = 2; length <= [1]; length++) { for (int i = 0; i <= n - length; i++) { int j = i + length - 1; // calculation code } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using n - 1 which misses the last chain length.
Using n + 1 which causes out of range errors.
✗ Incorrect
The chain length goes up to n, which is the number of matrices.
4fill in blank
hardFill both blanks to correctly update the minimum cost in dp table.
DSA C#
for (int k = i; k < j; k++) { int cost = dp[i, k] + dp[k + 1, j] + dims[i] * dims[k + 1] * dims[j + 1]; if (cost < dp[i, j]) { dp[i, j] = [1]; split[i, j] = [2]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning dp[i, j] to dp[i, k] instead of cost.
Storing cost instead of k in split array.
✗ Incorrect
We update dp[i, j] with the new minimum cost and record the split position k.
5fill in blank
hardFill all three blanks to complete the recursive function to print optimal parenthesization.
DSA C#
void PrintOptimalParens(int[,] split, int i, int j) {
if (i == j) {
Console.Write("A" + [1]);
return;
}
Console.Write("(");
PrintOptimalParens(split, i, [2]);
PrintOptimalParens(split, [3] + 1, j);
Console.Write(")");
}Attempts:
3 left
💡 Hint
Common Mistakes
Using j instead of i when printing matrix name.
Using split[j, i] which is incorrect indexing.
✗ Incorrect
When i == j, print matrix name with index i. Recursively print left and right parts using split[i, j].
