Magicsheet logo

Subrectangle Queries

Medium
73.1%
Updated 6/1/2025

Asked by 3 Companies

Subrectangle Queries

What is this problem about?

"Subrectangle Queries" is a medium-level design problem that asks you to implement a class to manage updates and queries on a 2D matrix. You need to support two operations:

  1. updateSubrectangle: Updates all values within a specified rectangular region (defined by row1, col1, row2, col2) to a new value.
  2. getValue: Returns the current value at a specific (row, col) coordinate. The challenge is to find an efficient way to handle these operations, especially if one is called much more frequently than the other.

Why is this asked in interviews?

Google and other companies ask this question to see how a candidate approaches trade-offs in data structure design. You can solve it by actually updating the matrix every time (making update slow but query fast) or by storing a list of updates (making update fast but query potentially slower). It evaluates your ability to consider different use cases and explain why one approach might be better than another depending on the frequency of operations.

Algorithmic pattern used

This is primarily a Matrix and Class Design problem. There are two main strategies:

  • Brute Force (Matrix Update): Every time updateSubrectangle is called, you iterate through the specified rows and columns and update the values in the 2D array. getValue is a simple O(1) lookup.
  • Optimization (Update History): You store the initial matrix and a list of all updateSubrectangle calls. To getValue, you iterate through the update history in reverse order. The first update that contains the requested coordinate provides the value. If no update covers it, you return the original matrix value.

Example explanation (use your own example)

Matrix: [[1,1], [1,1]]

  1. updateSubrectangle(0, 0, 1, 0, 5) -> The first column becomes 5. Matrix is now [[5, 1], [5, 1]].
  2. getValue(0, 0) -> Returns 5.
  3. updateSubrectangle(0, 0, 0, 1, 10) -> The first row becomes 10. Matrix is now [[10, 10], [5, 1]].
  4. getValue(0, 0) -> Returns 10. If using update history, we would see that the update to (0,0) with value 10 was the most recent.

Common mistakes candidates make

One common mistake is not asking the interviewer about the constraints or the expected frequency of operations. Without this, you cannot make an informed choice between the matrix-update and the history-list approaches. Another mistake is using an inefficient search through the history list (not going in reverse order), which would give you the wrong value if a coordinate was updated multiple times.

Interview preparation tip

When preparing for the Subrectangle Queries interview pattern, practice explaining the "Trade-off" clearly. "Approach A is better if we have many queries, while Approach B is better if we have many updates." This level of system-design-style thinking in a coding interview is highly valued. Also, ensure your loops in the brute-force approach are correctly bounded.

Similar Questions