Centered K-by-K Submatrix Sums
Updated
Problem statement
You are given a non-empty integer matrix matrix and a positive odd integer k. Construct an output matrix with the same dimensions.
Let radius = (k - 1) / 2. For every cell (row, col), sum all input cells inside the centered k-by-k window:
row - radius <= i <= row + radius
col - radius <= j <= col + radiusClip the window to valid matrix coordinates. Return the per-cell sums using signed 64-bit integers.
Function
centeredSubmatrixSums(matrix: int[][], k: int) → long[][]Examples
Example 1
matrix = [[1,2,3],[4,5,6],[7,8,9]]k = 3return = [[12,21,16],[27,45,33],[24,39,28]]Moved or new
The center cell sees the whole matrix and has sum 45. The top-left cell sees only [[1,2],[4,5]], whose sum is 12.
Example 2
matrix = [[2,-1,4],[0,3,5]]k = 1return = [[2,-1,4],[0,3,5]]When k = 1, each centered window contains only its own cell.
Example 3
matrix = [[1,2,3],[4,5,6]]k = 5return = [[21,21,21],[21,21,21]]Moved or new
The radius is 2, so after clipping, every window covers the complete matrix and has sum 21.
Constraints
1 <= matrix.length, matrix[0].length <= 1000.kis positive and odd.1 <= k <= 2 * max(matrix.length, matrix[0].length) + 1.-10^9 <= matrix[row][col] <= 10^9.- Every output sum fits in a signed 64-bit integer.
More Citadel Onsite Interview questions
- Maximize Workday EarningsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Minimum Changes for Palindromic BlocksOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Removal PoolONSITE INTERVIEW · Seen Sep 2026
- Search Suggestions with a Prefix TreeONSITE INTERVIEW · Seen Aug 2026
- Traverse Interior Matrix Cells in Row-Major OrderONSITE INTERVIEW · Seen Aug 2026
- Limit Order Book Matching EnginePHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Fixed-Capacity Circular BufferONSITE INTERVIEW · Seen Jun 2026
- Stock Trading with a Fee and a CooldownONSITE INTERVIEW · Seen May 2026
- Time-Based Key-Value StorePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2026
- 2048 Simulation and Board CodecONSITE INTERVIEW · Seen Jan 2026
Similar questions at other companies
- Range Sum Queries on a Fixed GridGoogle · ONSITE INTERVIEWGoogle interview questions
- Restaurant Delivery HeatmapDoorDash · ONSITE INTERVIEWDoorDash interview questions
- Number of Submatrices That Sum to TargetWhatNot · ONSITE INTERVIEWWhatNot interview questions
- Split a Matrix Into Four Partitions by Average (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Shortest Path Around Rectangular ObstaclesDRW · OADRW interview questions
- Sub-Matrix Sum QueriesHudson River Trading · OAHudson River Trading interview questions
- Largest Square Sub-Grid Within a Sum LimitC3.ai · OAC3.ai interview questions
- Matrix Region and Ring SumsNVIDIA · PHONE SCREENNVIDIA interview questions
- Count Non-Overlapping Pairs of Rectangles That Sum to KWisdomAI · PHONE SCREEN
- Maximum Sum RectangleDeloitte · OADeloitte interview questions