Sparse Matrix Multiplication
Updated
Problem statement
Given an integer matrix mat1 of size m × k and an integer matrix mat2 of size k × n, return their matrix product product, an m × n matrix. For every row i and column j:
product[i][j] = mat1[i][0] * mat2[0][j] + ... + mat1[i][k - 1] * mat2[k - 1][j]Many entries may be 0. The input and output are ordinary arrays that store every entry, but your multiplication should skip each term 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 of mat1 has one nonzero value, so its result row is 1 × [7,0,0]. The second row adds -1 × [7,0,0] and 3 × [0,0,1], which gives [-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 Apple Phone Screen questions
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
- Evaluate DivisionPHONE SCREEN · Seen Nov 2026
- Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
- Validate a BST with a Custom ComparisonPHONE SCREEN · Seen Nov 2026
- Design a Food Rating SystemPHONE SCREEN · Seen Oct 2026
- Days Until Poisoned Plants StabilizePHONE SCREEN · Seen Oct 2026
- Capture Surrounded Regions with Union-FindPHONE SCREEN · 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
Apple interview experiences
- Apple SWE Phone Screen: Subarray Products: 1 Experience
- Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
- Apple AI Coding and Related Interviews: 17 Experiences
- Apple India SWE New Grad Interviews: 5 Experiences
- Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
- Apple New Grad SWE and Embedded: 16 Experiences (Part 2 of 3)