Compute optimal matrix multiplication order
Company: XPeng
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates a candidate's understanding of dynamic programming, optimal substructure, and algorithmic optimization for matrix chain multiplication, including reconstructing an optimal parenthesization and adapting to variant cost models.
Constraints
- 2 <= len(dims) <= 200
- 1 <= dims[i] <= 10^4
- The number of matrices is n = len(dims) - 1
- Return a fully parenthesized string; matrices are named A1..An
Hints
- Let m[i][j] be the minimal cost to multiply Ai..Aj; try all splits k in [i..j-1].
- Transition: m[i][j] = min_k m[i][k] + m[k+1][j] + dims[i-1]*dims[k]*dims[j].
- Store the argmin split to reconstruct a parenthesization.
- Initialize m[i][i] = 0 and build solutions by increasing chain length.
- Top-down memoization yields the same recurrence if you prefer recursion.