Live Rectangle Sums Over a Heatmap That Receives Point Updates

Last updated: November 29, 2026

Quick Overview

Design a structure over an m x n heatmap of click counts that supports update(r, c, value) to overwrite a cell and sum(r1, c1, r2, c2) to return the total inside an inclusive rectangle, both in polylogarithmic time.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Design a structure over an m x n heatmap of click counts that supports update(r, c, value) to overwrite a cell and sum(r1, c1, r2, c2) to return the total inside an inclusive rectangle, both in polylogarithmic time.

A Google onsite Hard data-structure question tied to live click heatmaps. The interviewer expects you to reject static prefix sums because updates cost O(mn), then build a 2D Fenwick (binary indexed) tree and explain its lowbit mechanics.

What the Interviewer Expects
  • Explains why prefix sums fail under frequent updates
  • Implements a 2D Fenwick tree with nested lowbit loops
  • Converts overwrite updates into deltas
  • Answers rectangle sums with inclusion-exclusion over four prefix queries
  • States O(log m log n) per operation
Key Topics to Cover
Fenwick Tree
Binary Indexed Tree
Range Queries
Inclusion-Exclusion
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Support range updates (add v to a rectangle) with point queries.
  • The grid is huge and sparse; how do you avoid allocating m x n?
  • Compare with a 2D segment tree: when is each better?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Build from grid (m x n ints).
  • update(r, c, val): set the cell to val.
  • sum(r1, c1, r2, c2): inclusive rectangle total.
  • Interleaved operations, up to about 10^5 each....
High-Level Design

A 1D Fenwick tree stores at index i the sum of a block of length i & -i ending at i, so prefix sums and point updates each walk O(log n) indices. In 2D, nest the walks: tree[i][j] covers a block o...


Submit Your Answer
Markdown supported