Matrix Multiplication With and Without NumPy
Updated
Problem statement
Given two rectangular integer matrices a with shape R × K and b with shape K × C, return their matrix product as a new R × C matrix.
Each output cell is the dot product of one row of a and one column of b:
product[row][column] = sum(a[row][inner] * b[inner][column], inner = 0..K-1)- Keep rows and columns in their original order.
- Use exact integer multiplication and addition. Products and sums may exceed signed
32-bit range, so use signed64-bit arithmetic where required. - Do not modify either input matrix.
- All inputs have compatible dimensions and satisfy the stated constraints. Empty matrices, ragged rows, incompatible shapes and floating-point values are outside this exercise.
Two implementations
Implement the product manually without a matrix library. Also write the equivalent Python expression using NumPy for the same input and output contract. Both implementations must compute matrix multiplication, rather than elementwise multiplication. Your function returns the product. The editorial shows the NumPy version for comparison.
Function
multiplyMatrices(a: int[][], b: int[][]) → long[][]Examples
Example 1
a = [[1,2,3],[4,5,6]]b = [[7,8],[9,10],[11,12]]return = [[58,64],[139,154]]The first cell is 1 * 7 + 2 * 9 + 3 * 11 = 58. The two rows of a and two columns of b produce a 2 × 2 matrix.
Example 2
a = [[-1000000]]b = [[1000000]]return = [[-1000000000000]]The single product is -10^6 * 10^6 = -10^12. Its magnitude exceeds a signed 32-bit integer.
Example 3
a = [[2],[-3]]b = [[4,0,-5]]return = [[8,0,-10],[-12,0,15]]The shared dimension is 1. Each row of the product is one entry from a multiplied by the row [4,0,-5] from b.
Constraints
1 <= R, K, C <= 30.a.length = R, every row ofahasKentries,b.length = K, and every row ofbhasCentries.-10^6 <= a[row][inner], b[inner][column] <= 10^6.- Every individual product, partial sum and result fits a signed
64-bit integer; the absolute value of any partial sum is at most30 * 10^12.
More NVIDIA Phone Screen questions
- Network Layer Execution OrderPHONE SCREEN · Seen Nov 2026
- Paged KV Cache AccessPHONE SCREEN · Seen Nov 2026
- Sliding Window Attention over a KV CachePHONE SCREEN · Seen Nov 2026
- Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
- Hash Partition OffsetsPHONE SCREEN · Seen Oct 2026
- Filter Paginated Medical RecordsPHONE SCREEN · Seen Oct 2026
- Least Frequently Used CachePHONE SCREEN · Seen Oct 2026
- Parallel String Counts with a Custom Hash TablePHONE SCREEN · Seen Oct 2026
- Simulate a Reference-Counted Smart PointerPHONE SCREEN · Seen Aug 2026
- Streaming Top-K Frequent ElementsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- One-Nearest-Neighbor Classification with Manhattan DistanceOpenAI · PHONE SCREENOpenAI interview questions
- Multiply Two MatricesAdobe · PHONE SCREENAdobe interview questions
- Adjacent Order-Statistic Gap DistributionsScale AI · PHONE SCREENScale AI interview questions
- Scaled Dot-Product AttentionAmazon · PHONE SCREEN
- Rotate ImageTokopedia · PHONE SCREENTokopedia interview questions
- Rotate ImageSambaNova Systems · PHONE SCREENSambaNova Systems interview questions
- Survey Table: Group Given an AnswerSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Rotate a Flat Image ClockwiseGoogle · ONSITE INTERVIEWGoogle interview questions
- Matrix MultiplicationeBay · ONSITE INTERVIEWeBay interview questions
- Linear Layer Forward and Backward PassesMeta · ONSITE INTERVIEWMeta interview questions