PPractick

DSA interview question

Matrix Chain Multiplication (Bottom-Up DP)

Hard · Algorithms · Topics: dynamic programming, mcm, partition

The problem

You're optimizing a deep learning inference pipeline at an AI infra company. A forward pass requires multiplying a chain of weight matrices together: M1 × M2 × M3 × ... × Mn. Matrix multiplication is associative — the final result is the same regardless of how you parenthesize the multiplications — but the computational cost (number of scalar multiplications) varies wildly depending on the order you multiply them in. Your job: given the dimensions of each matrix in the chain, find the minimum number of scalar multiplications needed to compute the full product, using a bottom-up (iterative, not recursive) DP approach — this matters because the inference engine runs this optimization at startup on every model load, and recursion with memoization adds overhead and stack-depth risk for long chains.

Matrices are given via an array arr of dimensions, where matrix i (1-indexed from 1 to n-1) has dimensions arr[i-1] x arr[i]. So for n matrices, arr has n+1 elements.

Function signature

int matrixChainOrder(vector<int>& arr);

Test cases

Input arrOutputExplanation
[40,20,30,10,30]260004 matrices: 40×20, 20×30, 30×10, 10×30 — optimal parenthesization gives 26000 multiplications
[10,20,30]60002 matrices: 10×20, 20×30 — only one way to multiply → 102030 = 6000
[10,20,30,40,30]300004 matrices — optimal order yields 30000
[5,10]0Single matrix, no multiplication needed
[1,2,3,4,5,6]585 matrices, classic small textbook example

(Constraints typically: 1 <= arr.length <= 100, 1 <= arr[i] <= 500. Note: arr.length == n+1 for n matrices.)

How interviewers phrase it

Clarifying questions to ask

  1. Is arr guaranteed to represent a valid chain (i.e., arr.length == n+1 for n matrices, with consecutive dimensions compatible for multiplication)? (Yes by construction, since arr[i-1] x arr[i] defines matrix i.)
  2. Do you want just the minimum cost (scalar multiplication count), or also the actual optimal parenthesization (bracket placement)?
  3. Should I implement this strictly bottom-up (iterative table-filling), or is top-down memoization acceptable as a starting point before converting? (Confirm explicitly — bottom-up is the asked-for deliverable here.)
  4. What's the maximum chain length? (Up to ~100 matrices means O(n³) ≈ 1M operations, very feasible — confirms the standard O(n³) DP is the expected solution, not something more exotic.)

Follow-ups to expect

  1. "Can you reconstruct the optimal parenthesization (bracket string), not just the cost?"
  2. "Why is this O(n³) and not something faster?"
  3. "How does this relate to Burst Balloons / Minimum Cost to Cut a Stick / Palindrome Partitioning II?"
  4. "Can you convert your bottom-up solution back to top-down with memoization, and which is better here?"
  5. "What's the space complexity, and can you reduce it?"
  6. "What if some matrix multiplications are forbidden/restricted (e.g., a custom adjacency constraint)?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in