ByteDance interview question
Multiply two sparse matrices efficiently
- Company:
- ByteDance
- Type:
- Coding
- Difficulty:
- Medium
- Reported:
- November 18, 2026
Problem
Write a function that multiplies two sparse matrices, where most entries are zero.
Given matrix A of size m x k and matrix B of size k x n, return the product C = A * B of size m x n, where C[i][j] is the sum over p of A[i][p] * B[p][j].
The input representation is not specified; this version takes both matrices as 2D lists of integers and returns a 2D list. The goal is to avoid the work that the zero entries would cost in a plain triple loop.
Example 1
Input: A = [[1, 0, 0], [-1, 0, 3]], B = [[7, 0, 0], [0, 0, 0], [0, 0, 1]]
Output: [[7, 0, 0], [-7, 0, 3]]
Row 2 of C is -1 * row 1 of B plus 3 * row 3 of B.
Constraints
- Not stated; ask the interviewer.
Reported by candidates or drawn from publicly available sources, and rewritten in our own words. Pathwise Prep does not publish confidential assessment content. Report a concern