3. What will be the output of the following code snippet for p = [10, 20, 30] representing matrix dimensions?
def matrix_chain_order(p):
n = len(p) - 1
dp = [[0]*n for _ in range(n)]
for l in range(2, n+1):
for i in range(n-l+1):
j = i + l - 1
dp[i][j] = float('inf')
for k in range(i, j):
cost = dp[i][k] + dp[k+1][j] + p[i]*p[k+1]*p[j+1]
if cost < dp[i][j]:
dp[i][j] = cost
return dp[0][n-1]
print(matrix_chain_order(p))
medium
A. IndexError
B. 6000
C. None
D. 18000
Solution
Step 1: Trace the code execution
p=[10,20,30], n=2, dp=[[0,0],[0,0]]. l=2, i=0, j=1, dp[0][1]=inf. k=0: cost=dp[0][0]+dp[1][1]+10*20*30=0+0+6000. However, the code tries to access p[j+1] = p[2], which is valid here (p has length 3). So no IndexError here.
Step 2: Confirm output
Return dp[0][1] which is 6000.
Final Answer:
6000 -> Option B
Quick Check:
Cost = p[0]*p[1]*p[2] = 6000 [OK]
Hint: For chain of 2 matrices (n=2), cost is p[0]*p[1]*p[2] [OK]
Common Mistakes:
Anticipating IndexError on p[j+1]
Miscalculating cost (e.g., wrong indices)
Forgetting single k split for l=2
4. Find the bug in this Matrix Chain Multiplication code snippet:
def matrix_chain_order(p):
n = len(p) - 1
dp = [[0]*n for _ in range(n)]
for l in range(2, n+1):
for i in range(n-l+1):
j = i + l - 1
dp[i][j] = float('inf')
for k in range(i, j):
cost = dp[i][k] + dp[k+1][j] + p[i]*p[k]*p[j]
if cost < dp[i][j]:
dp[i][j] = cost
return dp[0][n-1]
medium
A. Return statement is missing
B. DP table initialization is wrong
C. Incorrect multiplication indices in cost calculation
D. Loop ranges are incorrect
Solution
Step 1: Review cost calculation formula
The cost formula should be p[i] * p[k+1] * p[j+1], but code uses p[i] * p[k] * p[j], which is incorrect indexing.
Step 2: Identify correct indices
Matrix dimensions are stored in p, so for multiplying matrices A_i..A_k and A_k+1..A_j, cost uses p[i], p[k+1], and p[j+1].
Final Answer:
Incorrect multiplication indices in cost calculation -> Option C
Quick Check:
Cost formula indices must be p[i], p[k+1], p[j+1] [OK]
Hint: Check dimension indices carefully in cost formula [OK]
Common Mistakes:
Using p[k] instead of p[k+1]
Mixing zero-based and one-based indexing
Ignoring matrix dimension rules
5. Given matrices with dimensions p = [5, 10, 3, 12, 5, 50, 6], what is the minimum number of scalar multiplications needed to multiply the entire chain?
hard
A. 1510
B. 2010
C. 4050
D. 3500
Solution
Step 1: Understand the input and problem
p has length 7, so there are 6 matrices with dimensions: 5x10, 10x3, 3x12, 12x5, 5x50, 50x6.
Step 2: Use dynamic programming to find minimum cost
By applying the Matrix Chain Multiplication DP algorithm, the minimum cost is calculated as 1510 scalar multiplications.
Final Answer:
1510 -> Option A
Quick Check:
DP minimum scalar multiplications = 1510 [OK]
Hint: Use DP table to store subproblem costs for efficiency [OK]
Common Mistakes:
Calculating cost manually without DP
Mixing matrix dimensions incorrectly
Forgetting to consider all parenthesization options
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.