OpenAI · ML & AI Fundamentals
Analyze matrix multiplication complexity
TrueInterview
October 7, 2026 · 3 min read
During an ML coding interview, you receive a PyTorch file and are asked several complexity questions about the operations it contains. One question is:
You are given two dense matrices and , with of shape and of shape , and you compute , the usual matrix product used by NumPy and PyTorch.
- In Big-O notation, what is the time complexity of this operation expressed in , , and ?
- What is the space complexity (additional memory) of this operation? Say explicitly whether the output matrix is included in that space.
Optional follow-up: How, if at all, does your answer change when and are batched—for example, has shape , has shape , and you perform a batched matmul?
Where to start: Write the formula for one output entry, . Count the work needed for that single entry, then count the total number of entries.
Time: Matrix multiplication is a triple loop over the two output dimensions and the shared contraction dimension. Because the three sizes are distinct, a one-letter bound like is incorrect; define each variable.
Space: Distinguish two quantities: the memory required to store the result versus the auxiliary scratch space the algorithm needs beyond its inputs and output. Ask whether accumulating requires any data structure that grows.
Constraints & Assumptions
- The matrices are dense, so there is no sparsity to exploit, and they use standard row- or column-major storage.
- You are analyzing the standard library algorithm that
A @ Bactually executes, not asymptotically faster sub-cubic methods; those may be mentioned, but they are not what@calls by default. - Count each scalar multiplication and addition as work, and set aside numerical precision and overflow issues for this analysis.
- For Big-O in terms of , , , keep the three dimensions separate; do not combine them into one variable unless you first say the matrices are square.
Clarifying Questions to Ask
- Do you want auxiliary (extra) space only, or total space including the output matrix ? Part 2 suggests the interviewer explicitly cares about this distinction.
- Should I assume the naive/standard algorithm, or do you also want sub-cubic approaches such as Strassen?
- Is the target CPU or GPU, and do you care about the exact FLOP count (the constant factor) or only the asymptotic class?
- Are the inputs guaranteed to be dense, or could sparsity alter the analysis?
What a Strong Answer Covers
- Defines the variables. Three distinct dimensions imply time , not a one-letter ; it becomes only after explicitly setting .
- Derives the time from first principles: there are output entries, each a dot product of length , giving , and shows this bound is tight for the standard algorithm.
- Separates output space from auxiliary space and states the convention: when is counted, otherwise auxiliary, since that distinction is precisely what part 2 requests.
- Connects the answer to FLOPs, about , which is the practical reason an ML interviewer asks this question: compute budgeting and model sizing.
- Knows the limit of the naive bound: acknowledges that Strassen and other sub-cubic algorithms exist, but states that NumPy, PyTorch, and BLAS run the cubic algorithm with optimized constants such as cache tiling, SIMD, and tensor cores, not a sub-cubic exponent.
- For batching, separates asymptotic behavior from throughput: time grows linearly in , so there is no Big-O improvement, but batched kernels are much faster in wall-clock time because of hardware utilization.
Follow-up Questions
- If the matrices were square, with , what is the complexity, and what is the smallest exponent you know any algorithm achieves in theory versus in practice?
- If only one operand were batched—for example, has shape but is a shared —can you avoid running separate matmuls? What would the complexity be then?
- Why does batched matmul (
torch.bmm) run much faster than a Pythonforloop over separate matmuls, even though both are ? - How would the time complexity change if were sparse with nonzeros rather than dense?
Overview: This question tests understanding of time and space complexity for matrix multiplication and memory accounting in ML workloads, covering asymptotic analysis, linear algebra operations, and resource estimation.