PPractick

DSA interview question

Matrix Chain Multiplication (Minimum Multiplications)

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

The problem

You're working on a rendering engine's transform pipeline. Every frame, the engine multiplies a chain of transformation matrices (model → world → view → projection, plus a few intermediate bone/skinning matrices) to get a final transform. Matrix multiplication is associative — ((AB)C)D gives the same result as A(B(CD)) — but the number of scalar multiplications needed depends heavily on the order you parenthesize the chain in.

Given an array dims of length n+1 representing n matrices, where matrix i has dimensions dims[i-1] x dims[i], find the minimum number of scalar multiplications needed to compute the full product.

Example: dims = [40, 20, 30, 10, 30] represents 4 matrices:

Find the cheapest parenthesization order.

Function signature

int matrixChainOrder(vector<int>& dims);

Test cases

Input dimsOutputExplanation
[40, 20, 30, 10, 30]26000Best split: (A1(A2A3))A4 or similar — verify via DP table
[10, 20, 30]6000Only one way to multiply 2 matrices: 10*20*30
[10, 20, 30, 40, 30]30000Classic textbook example
[1, 2, 3, 4]18Small chain, verify by brute-force enumeration
[5, 10]0Single matrix — no multiplication needed
[10]0Degenerate — no matrices (edge case, handle gracefully)

(Always sanity check: dims.size() == n+1 for n matrices; a single matrix needs 0 multiplications.)

How interviewers phrase it

Clarifying questions to ask

  1. Is the input given as an array of (rows, cols) pairs per matrix, or as a single dims array of length n+1? (Confirm the exact format before coding.)
  2. Do I need to return just the minimum cost, or also the actual parenthesization (bracketing)?
  3. Can n be large (checking if O(n³) DP is acceptable, or if I need to think about Catalan-number brute force being infeasible)?
  4. Are matrix dimensions guaranteed to be compatible (i.e., is the chain always valid so consecutive matrices can multiply)?
  5. Can any dimension be 0 or negative? (Edge case handling.)

Follow-ups to expect

  1. "Can you print the actual optimal parenthesization, not just the cost?"
  2. "What's the time and space complexity, and can you reduce space?"
  3. "Why not brute-force all parenthesizations?"
  4. "Can you solve this recursively with memoization first, then convert to tabulation?"
  5. "How does this relate to other interval DP problems?"
  6. "What if you wanted to minimize additions instead of multiplications, or minimize memory instead of cost?"

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