Code Room
CodingMediumcod-g211
Subject MatrixLevel Mid–Senior~25 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Given an m x n integer matrix mat and an integer k, return a matrix answer where answer[i][j] is the sum of all elements mat[r][c] with i-k <= r <= i+k and j-k <= c <= j+k, clamped to the matrix bounds. In other words, each output cell sums the (2k+1) x (2k+1) block centered on it, truncated at the edges. Values may be negative; the matrix has at least one row and column and k >= 0.

Implement
matrix_block_sum(mat: list[list[int]], k: int) → list[list[int]]
Examples
in[[[1,2,3],[4,5,6],[7,8,9]],1]out[[12,21,16],[27,45,33],[24,39,28]]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.