Subrectangle Queries

MediumMatrixdesign

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 needed

Problem

Implement SubrectangleQueries(rectangle), which wraps a rows x cols integer matrix and supports: - updateSubrectangle(row1, col1, row2, col2, newValue) sets every cell in the sub-rectangle with top-left corner (row1, col1) and bottom-right corner (row2, col2) (both inclusive) to newValue. - getValue(row, col) returns the current value at (row, col). Later updates overwrite earlier ones wherever they overlap. Coordinates always satisfy row1 <= row2 and col1 <= col2 and lie inside the matrix.

Examples

Input: ["SubrectangleQueries","getValue","updateSubrectangle","getValue","updateSubrectangle","getValue","getValue","getValue"] [[[[7,8],[9,6]]],[1,0],[0,0,0,1,3],[0,1],[0,1,1,1,4],[0,1],[1,1],[1,0]]
Output: [null,9,null,3,null,4,4,9]
The first update rewrites the top row to [3,3]. The second rewrites the right column to 4, giving [[3,4],[9,4]]. Cell (1,0) was never touched, so it stays 9.
Input: ["SubrectangleQueries","getValue","updateSubrectangle","getValue"] [[[[5]]],[0,0],[0,0,0,0,2],[0,0]]
Output: [null,5,null,2]
The single cell is 5 until it is overwritten with 2.

Constraints

  • 1 <= rows, cols <= 100
  • 1 <= rectangle[i][j], newValue <= 10^9
  • 0 <= row1 <= row2 < rows, 0 <= col1 <= col2 < cols
  • At most 500 calls in total

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.

More design problems