Count Rectangular Screen Regions Whose Engagement Delta Equals Target

Last updated: November 29, 2026

Quick Overview

A heatmap grid stores the change in tap engagement per screen cell after a UI experiment, with positive and negative integers. Count the rectangular sub-regions whose cells sum exactly to a target value.

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

0

0

0 solved


A heatmap grid stores the change in tap engagement per screen cell after a UI experiment, with positive and negative integers. Count the rectangular sub-regions whose cells sum exactly to a target value.

This is a hard matrix question TikTok interviewers use to see whether you can lift a 1D trick (prefix sums with a hash map) into 2D. They look for the O(R^2 C) solution, an argument for why sliding windows fail with negative values, and verification against a brute force.

What the Interviewer Expects
  • Explains why a two-pointer window fails when values can be negative
  • Recalls 1D subarray sum equals k with prefix sums and a hash map
  • Fixes a top and bottom row pair and compresses columns into a 1D array
  • Transposes so the squared factor is the smaller dimension
  • Cross-checks with a brute-force counter
Key Topics to Cover
2D prefix sums
hash maps
subarray sum equals k
matrix problems
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
  • How would you find the maximum-sum rectangle instead?
  • What if you need rectangles with sum at most the target?
  • The grid is 2000 by 2000; is O(R^2 C) feasible and what would you do instead?
  • How would you return one such rectangle's coordinates?
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

Grid of integers, possibly negative, up to roughly 100 by 100. Count all rectangles (any top-left and bottom-right pair) whose sum equals target. Output an integer count....

High-Level Design

In 1D, the number of subarrays summing to k is computed by walking a running prefix sum P and adding the number of earlier prefixes equal to P - k; a hash map holds prefix counts, seeded with `{0: 1...


Submit Your Answer
Markdown supported