KatInterview

Compute Complexity Bounds for Dense Matrix Operations

Math & Bit ManipulationAuto-graded · 18 test cases
HardAuto-gradedMath & Bit Manipulationcomplexity-analysislinear-algebrabig-o

Last updated

Tests asymptotic analysis skills by deriving time and space complexity for dense matrix multiplication with multiple dimensions.

You are analyzing the computational cost of dense matrix operations commonly found in numerical computing libraries.

Given two dense matrices with dimensions:

  • Matrix A: (m, n)
  • Matrix B: (n, p)

When computing their product C = A @ B using standard matrix multiplication:

Your task: Return a dictionary containing the Big-O complexity bounds for various aspects of this operation.

The dictionary must include these exact keys with string values in Big-O notation:

  1. "time": The time complexity of computing the product
  2. "auxiliary_space": Extra memory needed beyond inputs and output (working storage during computation)
  3. "output_space": Memory required to store the result matrix C
  4. "total_extra_space_including_output": Total additional memory beyond the input matrices
  5. "batched_time": Time complexity when matrices have a batch dimension (shapes (batch_size, m, n) and (batch_size, n, p))
  6. "batched_output_space": Memory for the batched result
  7. "batched_total_extra_space_including_output": Total extra memory for batched computation

Important assumptions:

  • Use the standard cubic algorithm (not Strassen or other sub-cubic methods)
  • Each scalar operation is O(1)
  • Matrices are dense with no exploitable sparsity
  • Express answers using the variable names exactly as they appear: m, n, p, batch_size
  • Use * for multiplication in the Big-O expressions
  • Batched operations process batch_size independent matrix multiplications

Constraints

  • 1 ≤ m, n, p ≤ 10,000
  • 1 ≤ batch_size ≤ 1,000
  • All values are positive integers
  • Return complexity strings in the exact format: "O(expression)" where expression uses * for products

Examples

Example 1

Input: matrix_multiply_complexity(2, 3, 4, 1)

Output: {'time': 'O(m*n*p)', 'auxiliary_space': 'O(1)', 'output_space': 'O(m*p)', 'total_extra_space_including_output': 'O(m*p)', 'batched_time': 'O(batch_size*m*n*p)', 'batched_output_space': 'O(batch_size*m*p)', 'batched_total_extra_space_including_output': 'O(batch_size*m*p)'}

Example 2

Input: matrix_multiply_complexity(100, 50, 25, 8)

Output: {'time': 'O(m*n*p)', 'auxiliary_space': 'O(1)', 'output_space': 'O(m*p)', 'total_extra_space_including_output': 'O(m*p)', 'batched_time': 'O(batch_size*m*n*p)', 'batched_output_space': 'O(batch_size*m*p)', 'batched_total_extra_space_including_output': 'O(batch_size*m*p)'}

Hints

Hint 1

For the output matrix with shape (m, p), how many elements must be computed?

Hint 2

Each output element requires a dot product of length n, involving n multiplications and n additions

Hint 3

Standard algorithms compute matrix multiplication in-place or with minimal temporary storage beyond the output buffer

Hint 4

For batched operations, consider whether the batch dimension changes the asymptotic complexity or just scales it linearly