Apply submatrix increment queries to an n x n matrix of zeros

Quick Overview

Start with an n x n matrix of zeros, apply up to ten thousand submatrix increment queries, and return the final matrix. The problem tests efficient range updates on a 2D grid and whether you can avoid touching every cell of every query.

Apply submatrix increment queries to an n x n matrix of zeros

Company: Adobe

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a positive integer `n`, indicating that we initially have an `n x n` **0-indexed** integer matrix `mat` filled with zeroes. You are also given a 2D integer array `queries`. For each `queries[i] = [row1, col1, row2, col2]`, do the following operation: - Add `1` to every element in the submatrix with the top left corner `(row1, col1)` and the bottom right corner `(row2, col2)`. That is, add `1` to `mat[x][y]` for all `row1 <= x <= row2` and `col1 <= y <= col2`. Return the matrix `mat` after processing all the queries. ### Function Signature ```python def range_add_queries(n: int, queries: list[list[int]]) -> list[list[int]]: ``` ### Rules - Each query is a list `[row1, col1, row2, col2]`. Both corners are inclusive. - Return the matrix as a list of `n` rows, each a list of `n` integers, with row `0` first. ### Constraints - `1 <= n <= 500` - `1 <= len(queries) <= 10^4` - `0 <= row1 <= row2 < n` - `0 <= col1 <= col2 < n` ### Examples ```text Input: n = 3, queries = [[1,1,2,2],[0,0,1,1]] Output: [[1,1,0],[1,2,1],[0,1,1]] ``` The first query increments the submatrix from (1,1) to (2,2), and the second increments the submatrix from (0,0) to (1,1). Cell (1,1) is covered by both queries, so its value is 2. ```text Input: n = 2, queries = [[0,0,1,1]] Output: [[1,1],[1,1]] ``` The single query covers the entire 2 x 2 matrix, so every cell becomes 1.

Overview: Start with an n x n matrix of zeros, apply up to ten thousand submatrix increment queries, and return the final matrix. The problem tests efficient range updates on a 2D grid and whether you can avoid touching every cell of every query.

Adobe logo
Adobe
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a positive integer n, indicating that we initially have an n x n 0-indexed integer matrix mat filled with zeroes.

You are also given a 2D integer array queries. For each queries[i] = [row1, col1, row2, col2], do the following operation:

  • Add 1 to every element in the submatrix with the top left corner (row1, col1) and the bottom right corner (row2, col2) . That is, add 1 to mat[x][y] for all row1 <= x <= row2 and col1 <= y <= col2 .

Return the matrix mat after processing all the queries.

Function Signature

def range_add_queries(n: int, queries: list[list[int]]) -> list[list[int]]:

Rules

  • Each query is a list [row1, col1, row2, col2] . Both corners are inclusive.
  • Return the matrix as a list of n rows, each a list of n integers, with row 0 first.

Constraints

  • 1 <= n <= 500
  • 1 <= len(queries) <= 10^4
  • 0 <= row1 <= row2 < n
  • 0 <= col1 <= col2 < n

Examples

Input: n = 3, queries = [[1,1,2,2],[0,0,1,1]]
Output: [[1,1,0],[1,2,1],[0,1,1]]

The first query increments the submatrix from (1,1) to (2,2), and the second increments the submatrix from (0,0) to (1,1). Cell (1,1) is covered by both queries, so its value is 2.

Input: n = 2, queries = [[0,0,1,1]]
Output: [[1,1],[1,1]]

The single query covers the entire 2 x 2 matrix, so every cell becomes 1.