Count Distinct Shapes of Flagged Regions in a Moderation Heatmap

Last updated: November 29, 2026

Quick Overview

Given a binary grid where 1 marks pixels flagged by a frame-level moderation model, return the number of distinct connected region shapes, where two regions match only if one is a translation of the other.

ByteDance
Coding & Algorithms
Software Engineer
ByteDance
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a binary grid where 1 marks pixels flagged by a frame-level moderation model, return the number of distinct connected region shapes, where two regions match only if one is a translation of the other.

ByteDance's trust and safety tooling groups flagged regions across video frames to spot repeated overlay patterns like watermarks or banned logos. This DFS question checks whether you can define a translation-invariant signature for each component.

What the Interviewer Expects
  • Find connected components with DFS or BFS
  • Normalize each component relative to an anchor cell
  • Use a canonical, order-independent key such as a sorted tuple
  • Explain why a raw DFS path string needs backtrack markers
  • State O(R * C log k) or O(R * C) time
Key Topics to Cover
DFS
connected components
canonical form
hashing
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
  • Treat rotations and reflections as the same shape.
  • How would you track shape counts across a stream of frames?
  • Grids are 4K resolution: what would you change for memory?
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: a grid of 0 and 1. Regions are 4-connected groups of 1s. Two regions are the same shape if one can be shifted, without rotation, to overlap the other exactly. Output: the number of distinct sha...

High-Level Design

Scan cells in row-major order. Each unvisited 1 is the top-left-most cell of a new region in scan order, so use it as the anchor. Run an iterative DFS, recording every cell as an offset from the ancho...


Submit Your Answer
Markdown supported