Multiply Two Sparse User by Feature Interaction Matrices
Last updated: November 29, 2026
Quick Overview
Given two mostly-zero matrices A (m x k) and B (k x n), such as user-by-feature and feature-by-ad interaction tables, return their product while skipping work on zero entries.
Meta
November 29, 20260
0
0 solved
Given two mostly-zero matrices A (m x k) and B (k x n), such as user-by-feature and feature-by-ad interaction tables, return their product while skipping work on zero entries.
A Meta technical screen question that tests whether you can exploit sparsity, which matters at Meta because recommendation features are overwhelmingly zero. The interviewer starts from the triple loop and expects you to reorder it so that zero entries of A are skipped and B is iterated only over its nonzeros. Follow-ups often move to storage formats like CSR.
What the Interviewer Expects
- Start from the O(mkn) definition and explain where the waste is
- Reorder loops to i, t, j so zero entries of A skip a whole inner loop
- Compress B into per-row nonzero lists
- State complexity in terms of nonzeros
- Discuss CSR and CSC formats and when each helps
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you store these matrices in CSR form and multiply in that form?
- What if both matrices are too large for one machine?
- How does the cost compare to dense multiplication on a GPU?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Aism x k,Bisk x n, each up to 100 x 100 in the screen version but far larger in production.- Most entries are zero.
- Return the dense
m x nproduct....
High-Level Design
C[i][j] = sum_t A[i][t] * B[t][j]. Instead of computing each C[i][j] with a loop over t, iterate i, then each nonzero A[i][t], then each nonzero B[t][j], and accumulate into C[i][j]. Eve...