Implement a sparse matrix multiplier class that skips zero entries
Company: Naver
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
Given two **sparse matrices** `mat1` of size `m x k` and `mat2` of size `k x n`, return the result of `mat1 x mat2`. You may assume that multiplication is always valid.
A sparse matrix is a matrix in which most of the elements are zero. The solution should take advantage of sparsity to avoid unnecessary multiplications.
Implement a class `SparseMatrixMultiplier`:
- `SparseMatrixMultiplier(mat1: List[List[int]], mat2: List[List[int]])`: Store the two matrices.
- `multiply() -> List[List[int]]`: Return the result of multiplying `mat1` by `mat2` as an `m x n` matrix. Exploit sparsity by skipping zero elements.
Example 1:
```text
mult = SparseMatrixMultiplier([[1, 0, 0], [-1, 0, 3]], [[7, 0, 0], [0, 0, 0], [0, 0, 1]])
mult.multiply() -> [[7, 0, 0], [-7, 0, 3]]
```
Row 0 of `mat1` has a single nonzero, 1 in column 0, so row 0 of the product equals row 0 of `mat2`. Row 1 is -1 times row 0 of `mat2` plus 3 times row 2 of `mat2`, which gives `[-7, 0, 3]`.
Example 2:
```text
mult = SparseMatrixMultiplier([[0]], [[0]])
mult.multiply() -> [[0]]
```
Both matrices are entirely zero, so the product is a 1 x 1 zero matrix.
```hint Loop order
In the textbook i, j, p loop, a zero in mat1 still costs a full pass over the shared dimension. Think about which loop order lets a single zero test on mat1[i][p] skip a whole run of work.
```
```hint Zeros on the other side
If mat2 is also mostly zeros, an inner loop over every column still visits them all. Consider what you could record about each row of mat2 once, before multiplying.
```
### Constraints & Assumptions
- `m == mat1.length`
- `k == mat1[i].length == mat2.length`
- `n == mat2[i].length`
- `1 <= m, n, k <= 100`
- `-100 <= mat1[i][j], mat2[i][j] <= 100`
### Clarifying Questions to Ask
- Are the matrices always given as dense lists of rows, or could they arrive as lists of nonzero entries?
- Is `mat2` sparse too, or only `mat1`?
- Will the same matrices be multiplied more than once, so that preprocessing in the constructor pays off?
- Should the result be the full `m x n` matrix, or only its nonzero entries?
### What a Strong Answer Covers
- Why reordering the loops lets a zero in `mat1` skip a whole block of work, and why the sums stay the same.
- Correct code that returns an `m x n` matrix, including when every entry is zero.
- Time complexity in terms of the number of nonzero entries, and the dense worst case.
- How to take advantage of zeros in `mat2` as well as in `mat1`.
### Follow-up Questions
- If the matrices were too large to store densely, what format would you keep them in, and how would multiplication work on it?
- The same `mat2` is multiplied by many different `mat1`. What would you precompute, and where?
- How would you split this work across several threads or machines?
Overview: Implement a class that multiplies an m by k matrix by a k by n matrix when most entries are zero and returns the full product. It tests whether you can use sparsity to avoid wasted multiplications and state the running time in terms of the number of nonzero entries.