Matrix Block Sum
MediumMatrixprefix sum
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
Given an m x n integer matrix mat and an integer k, build a matrix answer of the same size where answer[i][j] is the sum of all mat[r][c] with i - k <= r <= i + k and j - k <= c <= j + k, counting only cells that lie inside the matrix.
Return answer.
Examples
Input: mat = [[1,2,3],[4,5,6],[7,8,9]], k = 1
Output: [[12,21,16],[27,45,33],[24,39,28]]
For the center cell the 3x3 block is the whole matrix (sum 45); corners only see a 2x2 block, e.g. `1+2+4+5 = 12`.
Input: mat = [[1,2,3],[4,5,6],[7,8,9]], k = 2
Output: [[45,45,45],[45,45,45],[45,45,45]]
With `k = 2` every block covers the entire matrix.
Constraints
- 1 <= m, n <= 100
- 1 <= k <= 100
- 1 <= mat[i][j] <= 100
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.