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.
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
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- 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 ProblemsSample Answer
Requirements
- Input: grid
g(m rows, n columns) of integers, and integertarget. - 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...