Range Sum Query in a Mutable Grid
Updated
Problem statement
You are given an m x n grid of integers matrix. Rows and columns are numbered from 0. You also get a list of operations to run in order: operations[i] is the name of operation i, and arguments[i] holds its numbers.
"update"with[row, col, val]: set the cell in rowrowand columncoltoval. The old value is replaced, not added to."sumRegion"with[row1, col1, row2, col2]: add up every cell of the rectangle whose top-left cell is in rowrow1, columncol1and whose bottom-right cell is in rowrow2, columncol2. The rows and columns of both corners are included.
Each "sumRegion" uses the grid as it is at that moment, after every earlier update. Return a list with the result of every "sumRegion" operation, in the order they run. If there is none, return an empty list.
Function
numMatrix(matrix: int[][], operations: String[], arguments: int[][]) → int[]Examples
Example 1
matrix = [[2,-1,4],[0,3,5]]operations = ["sumRegion","update","sumRegion","sumRegion"]arguments = [[0,0,1,1],[1,1,-2],[0,0,1,1],[0,1,1,2]]return = [4,-1,6]The first rectangle covers rows 0 to 1 and columns 0 to 1: 2 + (-1) + 0 + 3 = 4.
The update sets the cell in row 1, column 1 from 3 to -2. Now the same rectangle is 2 + (-1) + 0 + (-2) = -1.
The last rectangle covers rows 0 to 1 and columns 1 to 2: (-1) + 4 + (-2) + 5 = 6. The answer is [4,-1,6].
Example 2
matrix = [[7]]operations = ["update","update","sumRegion"]arguments = [[0,0,1],[0,0,-9],[0,0,0,0]]return = [-9]Each update replaces the value; it does not add to it. After both updates the only cell holds -9, so the answer is [-9].
Example 3
matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]operations = ["sumRegion","sumRegion"]arguments = [[1,2,2,3],[0,0,2,0]]return = [38,15]Rows 1 to 2 and columns 2 to 3 hold 7, 8, 11 and 12, which add up to 38. Column 0 of every row holds 1, 5 and 9, which add up to 15. The answer is [38,15].
Constraints
1 <= m, n <= 100, wherem == matrix.lengthandn == matrix[i].length- Every row of
matrixhas the same length. -1000 <= matrix[i][j] <= 10001 <= operations.length <= 1000arguments.length == operations.length- Each
operations[i]is"update"or"sumRegion". - For
"update",arguments[i] = [row, col, val]with0 <= row < m,0 <= col < nand-1000 <= val <= 1000. - For
"sumRegion",arguments[i] = [row1, col1, row2, col2]with0 <= row1 <= row2 < mand0 <= col1 <= col2 < n.
More Bloomberg LP Onsite Interview questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent WordsONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Decode StringPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Minimum Replacements to Make Two Strings AnagramsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Currency Conversion RatePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Rectangular K-in-a-RowDatabricks · ONSITE INTERVIEWDatabricks interview questions
- Prefix Matrix Products and AutogradOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Out-of-Order Logger Rate LimiterGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Dynamic Fixed-K Score LeaderboardCoinbase · ONSITE INTERVIEWCoinbase interview questions
- Cats, Rabbits, and Snails Board GameAsana · ONSITE INTERVIEW
- Dynamic Weighted SamplingCitadel · ONSITE INTERVIEWCitadel interview questions
- Volunteer Awards: Totals Divisible by KNutanix · ONSITE INTERVIEWNutanix interview questions
- Maintain a Consistent-Hashing RingCohesity · ONSITE INTERVIEW
- Tic-Tac-Toe Move WinnersByteDance · ONSITE INTERVIEWByteDance interview questions
- Restaurant Delivery HeatmapDoorDash · ONSITE INTERVIEWDoorDash interview questions
Bloomberg LP interview experiences
- Bloomberg SWE Phone Screen and Onsite: 1 Experience
- Bloomberg SWE Phone Screens with Two Teams: 1 Experience
- Bloomberg AI Interview, Hiring Level Not Reported: 2 Experiences
- Bloomberg Experienced SWE AI Coding Interviews: 2 Experiences
- Bloomberg New Grad AI-Assisted Coding Rounds: 3 Experiences
- Bloomberg New Grad SWE AI-Assisted Coding Rounds: 1 Experience