Sparse Matrix Multiplication
Updated
Problem statement
You are given two rectangular integer matrices, mat1 with dimensions m × k and mat2 with dimensions k × n.
Return their matrix product product, an m × n matrix. For every valid row i and column j:
product[i][j] = mat1[i][0] * mat2[0][j] + ... + mat1[i][k - 1] * mat2[k - 1][j]Sparse-matrix requirement
Many entries may be 0. Although the matrices use ordinary dense arrays for input and output, design the multiplication so it skips contributions whose left or right factor is 0.
Function
multiply(mat1: int[][], mat2: int[][]) → int[][]Examples
Example 1
mat1 = [[1,0,0],[-1,0,3]]mat2 = [[7,0,0],[0,0,0],[0,0,1]]return = [[7,0,0],[-7,0,3]]The first row has only one nonzero value, so it contributes 1 × [7,0,0]. The second row contributes -1 × [7,0,0] and 3 × [0,0,1], producing [-7,0,3].
Example 2
mat1 = [[0,0],[0,0]]mat2 = [[1,2],[3,4]]return = [[0,0],[0,0]]Every value in mat1 is 0, so there are no nonzero contributions to the product.
Example 3
mat1 = [[1,2],[0,3]]mat2 = [[4,0,5],[6,7,0]]return = [[16,14,5],[18,21,0]]For example, product[0][0] = 1 × 4 + 2 × 6 = 16, while product[1][2] = 0 × 5 + 3 × 0 = 0.
Constraints
1 ≤ m, k, n ≤ 100.mat1.length = mand every row ofmat1has lengthk.mat2.length = kand every row ofmat2has lengthn.-100 ≤ mat1[i][t], mat2[t][j] ≤ 100.- The product of the two matrices fits in a signed 32-bit integer matrix.
More Bloomberg LP Phone Screen questions
- Pascal's TrianglePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Valid Triangle NumberPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Collatz Conjecture with Shared MemoizationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
- Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
- Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- 2D Convolution Layer Forward PassWaymo · PHONE SCREENWaymo interview questions
- Matrix EscapeByteDance · PHONE SCREENByteDance interview questions
- Minimum Time to Spread Through a GridAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Max Area of Island Without Changing the GridFlexport · PHONE SCREENFlexport interview questions
- Sudoku Board ValidationConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
- Reachable Exits in an Obstacle GridWhatNot · PHONE SCREENWhatNot interview questions
- Apply a 2048 Board MoveGlean · PHONE SCREENGlean interview questions
- Spiral Order with ObstaclesTemu · PHONE SCREENTemu interview questions
- Find Robots by Blocker DistancesUber · PHONE SCREENUber interview questions
- Incremental Hard Attention with a KV CacheOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
Bloomberg LP interview experiences
- Bloomberg AI-Assisted Coding Round, All Levels: 14 Experiences
- Bloomberg SWE Intern Interview Questions: 68 Questions
- Bloomberg Interview Questions, Level Not Reported: 65 Questions
- Bloomberg Experienced Design & More: 126 Questions (Part 2 of 2)
- Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
- Bloomberg New Grad Coding Questions: 121 Questions (Part 2 of 2)