Count Heatmap Rectangles Whose Cell Totals Hit a Target

Last updated: November 29, 2026

Quick Overview

Given an m x n grid of integer click counts from a page heatmap (values may be negative after baseline subtraction) and a target, count the axis-aligned sub-rectangles whose cells sum to exactly the target.

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

0

0

0 solved


Given an m x n grid of integer click counts from a page heatmap (values may be negative after baseline subtraction) and a target, count the axis-aligned sub-rectangles whose cells sum to exactly the target.

A Google onsite extension of the 1D subarray-sum-equals-k idea into two dimensions, framed around analyzing click heatmaps for a Search results page. The interviewer wants you to fix a pair of rows, collapse columns into a 1D array, and reuse the prefix-sum hash map.

What the Interviewer Expects
  • Reduces 2D to 1D by fixing top and bottom rows
  • Maintains column sums incrementally as the bottom row moves
  • Applies prefix sum plus hash map counting on the 1D array
  • Picks the smaller dimension for the outer loops
  • States O(min(m,n)^2 * max(m,n)) time
Key Topics to Cover
2D Prefix Sums
Hash Map
Dimension Reduction
Subarray Sum
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
  • Find the maximum-sum rectangle with sum at most K.
  • What if all values are non-negative; can you use two pointers instead?
  • How would you handle a very tall, narrow heatmap?
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
  • Input: grid g (m rows, n columns) of integers, and integer target.
  • Output: number of sub-rectangles with sum equal to target.
  • m, n up to about 100 to 300....
High-Level Design

A rectangle is defined by a top row, a bottom row, a left column and a right column. Fix top, then extend bottom downward one row at a time, maintaining col[c], the sum of column c between top a...


Submit Your Answer
Markdown supported