Dense Matrix Multiplication
Updated
Problem statement
You are given two rectangular integer matrices. left has m rows and k columns. right has k rows and n columns. Return their matrix product, which has m rows and n columns.
Each entry of the result uses one row of left and one column of right. Multiply them element by element and add up the products: result[i][j] = sum(left[i][t] * right[t][j]) for 0 <= t < k.
Optimization follow-up
Once your simple version is correct, explain how tiling reuses a small block of data. Also explain how reading memory in one contiguous run, one element after the next, affects throughput.
Then discuss data coalescing: how GPU threads that sit next to each other would read elements that sit next to each other. This part is discussion only. You do not need any GPU API, kernel launch, timing target, or hardware-specific code.
Function
multiplyMatrices(left: int[][], right: int[][]) → int[][]Examples
Example 1
left = [[1,2],[3,4]]right = [[5,6],[7,8]]return = [[19,22],[43,50]]Both matrices are two by two, so the result is two by two as well. Each entry multiplies one row of left with one column of right, pair by pair, and adds the products. For the top-left entry, one times five plus two times seven gives nineteen. The other three entries are found the same way.
Example 2
left = [[2],[3]]right = [[4,5,6]]return = [[8,10,12],[12,15,18]]left has one column and right has one row, so each entry is a single product. The first row of the result is the first value of left times each value of right. The second row does the same with the second value of left. This is called an outer product.
Example 3
left = [[1,0,-2]]right = [[3],[4],[5]]return = [[-7]]left is one row and right is one column, so the result has a single entry. Multiply in pairs and add: one times three, plus zero times four, plus minus two times five, gives minus seven.
Constraints
1 <= m, k, n <= 64.- Both matrices are rectangular and the inner dimensions agree.
- All elements are integers between
-100and100. - Every partial sum and result fits a signed 32-bit integer.
More Amazon Phone Screen questions
- Count Connected ComponentsPHONE SCREEN · Seen Dec 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Merge Accounts by Shared AttributePHONE SCREEN · Seen Dec 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Merge k Sorted ListsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Search in a Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Days Until Poisoned Plants StabilizeApple · PHONE SCREENApple interview questions
- Apply a 2048 Board MoveGlean · PHONE SCREENGlean interview questions
- File-Backed Dense Game of LifeApplied Intuition · PHONE SCREENApplied Intuition interview questions
- Tilting Maze BallHudson River Trading · PHONE SCREENHudson River Trading interview questions
- Spiral Order with ObstaclesTemu · PHONE SCREENTemu interview questions
- Logistic Regression DiagnosticsLinkedIn · PHONE SCREENLinkedIn interview questions
- Spiral Matrix TraversalByteDance · PHONE SCREENByteDance interview questions
- S-Shaped Matrix TraversalFox Corporation · PHONE SCREEN
- Longest Consecutive Same-Color LineGoogle · PHONE SCREENGoogle interview questions
Amazon interview experiences
- Amazon Experienced SDE Onsite Questions: 1 Experience
- Amazon SDE Onsite Interview Questions: 9 Experiences
- Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
- Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
- Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
- Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)