Compute optimal matrix-chain multiplication order
Company: Apple
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Quick Answer: This question evaluates dynamic programming, algorithmic analysis, and sensitivity reasoning for matrix-chain multiplication, and it belongs to the Coding & Algorithms domain; it is commonly asked because it tests understanding of optimal substructure, trade-offs in parenthesization, and correctness versus greedy heuristics.
Constraints
- 2 <= len(p) <= 1000 (so 1 <= n <= 999 matrices); for len(p) <= 2 the answer is 0
- 1 <= p[i] (each dimension is a positive integer)
- The product can grow large; use a 64-bit integer accumulator in languages with fixed-width ints (e.g. C++/Java).
Examples
Input: ([10, 30, 5, 60, 2, 100],)
Expected Output: 3500
Explanation: The original Apple instance. Optimal order ((A1*(A2*(A3*A4)))*A5) costs 600+300+600+2000 = 3500.
Input: ([5, 10, 3, 12, 5],)
Expected Output: 405
Explanation: A1(5x10),A2(10x3),A3(3x12),A4(12x5). Optimal is (A1*A2)*(A3*A4): 150 + 180 + 75 = 405, which beats A1*(A2*(A3*A4)) at 580.
Input: ([40, 20, 30, 10, 30],)
Expected Output: 26000
Explanation: Classic CLRS-style instance; the DP yields 26000 as the minimum scalar multiplications.
Input: ([10, 20, 30],)
Expected Output: 6000
Explanation: Only one way to multiply two matrices A1(10x20),A2(20x30): cost 10*20*30 = 6000.
Input: ([10, 20],)
Expected Output: 0
Explanation: A single matrix (n=1): nothing to multiply, so the cost is 0.
Input: ([5],)
Expected Output: 0
Explanation: Degenerate input with no matrix (n=0): answer is 0.
Input: ([10, 30, 5, 60],)
Expected Output: 4500
Explanation: Prefix sub-instance A1(10x30),A2(30x5),A3(5x60). Optimal (A1*A2)*A3: 1500 + 10*5*60 = 1500+3000 = 4500.
Hints
- Let m[i][j] be the minimum cost to multiply matrices Ai..Aj. Then m[i][i] = 0.
- For i < j, try every split point k in [i, j-1]: m[i][j] = min over k of m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j]. The last term is the cost of multiplying the two resulting (p[i-1] x p[k]) and (p[k] x p[j]) blocks.
- Fill the table by increasing chain length so that all smaller subchains are already solved before you need them.
- Greedy (always multiply the locally cheapest adjacent pair) does NOT work — a locally cheap product can leave an awkward shape that inflates later costs.