Code Room
CodingHardcod-g309
Subject Fenwick treeLevel Senior–Staff~40 minCommon in Algorithms & data structures interviewsIndustries Software development, Technology

Question

Maintain a rows x cols matrix initialized to all zeros using a 2D Fenwick (binary indexed) tree. Process operations: ["update", r, c, v] adds v to cell (r, c), and ["query", r1, c1, r2, c2] returns the sum of the inclusive submatrix with top-left (r1, c1) and bottom-right (r2, c2). All indices are 0-based and in range. Return the list of query answers in order.

Implement
fenwick_2d(rows: int, cols: int, ops: list[list]) → list[int]
Examples
in[3,3,[["update",0,0,5],["update",2,2,3],["query",0,0,2,2],["query",1,1,2,2]]]out[8,3]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.