Range Sum Query 2D - Immutable
A medium-tier problem at 57% community acceptance, tagged with Array, Design, Matrix. Reported in interviews at Lyft and 5 others.
Range Sum Query 2D is the matrix version of prefix sums, and it shows up in assessments at Lyft, Nvidia, DoorDash, and others. You're given a 2D matrix and need to answer repeated range-sum queries fast. The naive approach (loop through the rectangle every query) times out. The trick is building a 2D prefix sum table during initialization so each query runs in constant time. About 57% of candidates get it right, which means the pattern isn't obvious if you haven't built a 2D prefix structure before. If you hit this on your OA and blank on the 2D logic, StealthCoder surfaces the solution invisibly.
Companies that ask "Range Sum Query 2D - Immutable"
Range Sum Query 2D - Immutable is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know.
Get StealthCoderThe core insight is extending the 1D prefix-sum idea to two dimensions. You precompute a DP table where each cell holds the sum of all elements in the rectangle from the top-left corner to that cell. The recurrence is DP[i][j] = matrix[i][j] + DP[i-1][j] + DP[i][j-1] - DP[i-1][j-1]. The subtraction cancels the overlap when adding horizontal and vertical strips. Then any range sum from (r1,c1) to (r2,c2) is just DP[r2][c2] - DP[r1-1][c2] - DP[r2][c1-1] + DP[r1-1][c1-1]. Common trap: off-by-one errors in the recurrence or query formula. The problem tests your ability to think in 2D and handle boundary conditions cleanly. This is a design problem because you're architecting for repeated queries, not a one-off computation.
Pattern tags
You know the problem.
Make sure you actually pass it.
Range Sum Query 2D - Immutable recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Range Sum Query 2D - Immutable interview FAQ
Why is 2D prefix sum harder than 1D?+
In 1D you add left and right; in 2D you're managing four overlapping rectangles. The inclusion-exclusion principle requires adding the main rectangle, subtracting two strips, then adding back the corner you subtracted twice. Most candidates forget or mess up the sign on that final term.
How do companies like Nvidia and DoorDash use this pattern in real code?+
Image processing, heatmaps, and region-based analytics all rely on 2D range queries. Fast prefix sums are the backbone. If you're pre-processing static data and answering many queries, this design wins over naive recomputation.
What's the gotcha with boundary conditions?+
You need to pad your DP table with a row and column of zeros at the start so DP[i-1][j] and DP[i][j-1] don't go out of bounds. Without padding, your recurrence and query formula break. Most wrong submissions fail here.
Is this a pure algorithm problem or a design one?+
Both. The algorithm is the 2D prefix formula, but the design part is that you initialize once (heavy upfront work) then answer queries in O(1). The problem tests whether you optimize for the right constraint: many repeated queries, not repeated construction.
Will I see this at Lyft or Upstart?+
Yes, companies report asking it. It's not the hardest medium, but it's a classic test of whether you know prefix sums well enough to extend them to 2D. If you've only drilled 1D, the jump catches you off guard in a live OA.
Want the actual problem statement? View "Range Sum Query 2D - Immutable" on LeetCode →