Cheapest Evaluation Order for a Chain of GEMMs

Last updated: November 29, 2026

Quick Overview

Given dimensions d0..dn for a chain of matrices where matrix i is d_i by d_{i+1}, find the multiplication order that minimizes total multiply-add operations, and return both the cost and the parenthesization.

NVIDIA
Coding & Algorithms
Software Engineer
NVIDIA
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Given dimensions d0..dn for a chain of matrices where matrix i is d_i by d_{i+1}, find the multiplication order that minimizes total multiply-add operations, and return both the cost and the parenthesization.

Shows up in NVIDIA onsites for cuBLAS, cuTENSOR and deep learning compiler teams, where choosing a contraction order is a real optimization (einsum path finding is the generalized form). The interviewer wants a correct interval DP, a reconstruction of the order, and discussion of when FLOP count is not the full story on a GPU.

What the Interviewer Expects
  • Defines the subproblem cost[i][j] for multiplying matrices i through j
  • Writes the recurrence over split points k with the dims[i] * dims[k+1] * dims[j+1] merge cost
  • Fills the table by increasing chain length
  • Stores split points and reconstructs the parenthesization
  • Discusses memory traffic, Tensor Core shape efficiency, and intermediate size as extra costs
Key Topics to Cover
interval dynamic programming
matrix chain multiplication
reconstruction
GEMM
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you change the objective to minimize peak intermediate memory?
  • Generalize this to an einsum with arbitrary tensor contractions. Is DP still tractable?
  • Is there a faster than O(n^3) algorithm for this specific problem?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: list dims of length n+1. Multiplying a p x q by q x r costs pqr multiply-adds (2pqr FLOPs). Output the minimum total and a string like ((M0 x M1) x M2). Confirm n is at least 1 and dims a...

High-Level Design

Any order has a last multiplication that splits the chain at some k into [i..k] and [k+1..j]. The left result is dims[i] x dims[k+1] and the right is dims[k+1] x dims[j+1], so `cost[i][j] = mi...


Submit Your Answer
Markdown supported