DSA Tracker

Hard

Matrix Chain Multiplication

A hard Dynamic Programming problem included in Love Babbar 450, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Dynamic Programming
Sheets
2
Core for
9 roles
Platform
GeeksforGeeks

The problem

Given a sequence of matrices, find the most efficient way to multiply them together. The cost is determined by the number of scalar multiplications. Return the minimum number of multiplications needed.

Example 1

Input
dims = [10,30,5,60] (matrices: 10x30, 30x5, 5x60)
Output
4500
Why
Optimal parenthesization is (A1(A2A3)) costing 30*5*60 + 10*30*60 = 9000+18000... Actually (A1A2)A3 costs 10*30*5 + 10*5*60 = 1500+3000=4500.

Example 2

Input
dims = [40,20,30,10,30] (matrices: 40x20, 20x30, 30x10, 10x30)
Output
26000
Why
The optimal parenthesization minimizes scalar multiplications to 26000.

Example 3

Input
dims = [10,20,30] (single matrix 10x30)
Output
0
Why
Only one matrix, no multiplication needed.

Constraints

  • 2 <= dims.length <= 100
  • 1 <= dims[i] <= 500

How to think about it

Updated 2026-09-09

Multiplying an entire chain of matrices from i to j requires choosing one final split point k where the product of chain (i..k) multiplies the product of chain (k+1..j). The scalar cost of that outer multiplication is dims[i-1] * dims[k] * dims[j], plus the optimal sub-costs of both halves.

Approaches, worst first

  1. Exhaustive parenthesization

    time O(4^n / n^(3/2)) · space O(n)

    Try all split points k recursively for every range (i, j). The number of valid parenthesizations is the Catalan number C(n-1), which grows exponentially with n.

  2. Interval dynamic programmingWrite this one

    time O(n^3) · space O(n^2)

    Compute optimal costs for chain lengths from 2 to n. For chain (i, j), test all split points k from i to j - 1: dp[i][j] = min(dp[i][k] + dp[k+1][j] + dims[i-1] * dims[k] * dims[j]).

Where people lose marks · 3
  • Off-by-one errors with matrix indices and dimension array indices. Matrix i (1-indexed) has dimensions dims[i-1] x dims[i].
  • A single matrix (dims.length == 2) requires 0 scalar multiplications; base cases where i == j must be explicitly set to 0.
  • Incorrect loop ordering: calculating dp[i][j] before its constituent sub-intervals (i, k) and (k+1, j) have been fully solved.

The theory behind it

Dynamic Programming — the ground this problem stands on. All Dynamic Programming problems

What Dynamic Programming is

Dynamic programming is a method for solving a complex problem by breaking it into overlapping subproblems, solving each subproblem only once, and remembering the answers in a lookup table. Instead of recalculating identical questions over and over, future steps look up previous answers directly. By assembling these saved pieces from the bottom up or storing them during recursion, a task that would take billions of steps finishes in a fraction of a second.

When to reach for it

Reach for dynamic programming when questions ask for the maximum profit, minimum cost, total number of distinct ways to achieve a goal, or whether a target can be formed. Signals include overlapping choices where making a choice now affects what choices remain later, but greedy picking fails to find the true global optimum. If drawing a recursive decision tree reveals the same subproblem states repeating across branches, dynamic programming is needed.

How the pattern works

Identify the state variables that uniquely describe a subproblem, such as an array index and remaining capacity. Write the base cases first, representing states whose answers are known without calculation. Next, write the recurrence relation that expresses the current state using previously solved states, taking the minimum, maximum, or sum among your options. Build the solution either top-down by caching recursive returns in a memo table, or bottom-up by filling an array in topological dependency order. When each state depends only on the previous row, compress storage down to a single array.

What each operation costs

OperationTime
fill dynamic programming table of n statesO(n)
solve two-dimensional grid of m by n statesO(m * n)
space-optimized state transition keeping one rowO(n)
What usually goes wrong with Dynamic Programming
  • Filling a bottom-up table in an order where the current cell needs values that have not been computed yet, reading uninitialized zeros.
  • Failing to initialize base cases properly, such as filling a minimization table with zeros instead of infinity, which traps the answer at zero.
  • Overwriting values in a 1D space-optimized knapsack array by scanning in the wrong direction, allowing the same item to be chosen multiple times.

Which roles need this problem

Dynamic Programming is a core topic for these 9 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 5 more roles, including Game Developer, Cryptography Engineer, Performance Engineer.

Companies that have asked it

Tags taken from the problem's own GeeksforGeeks page — not a copied list.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Dynamic Programming problems

Problem set and role mapping as of .