Count Distinct Island Shapes on a Grid Up to Translation
Last updated: November 29, 2026
Quick Overview
Given a binary grid, count the number of distinct island shapes, where two islands are the same if one can be translated (not rotated or reflected) to match the other exactly.
0
0
0 solved
Given a binary grid, count the number of distinct island shapes, where two islands are the same if one can be translated (not rotated or reflected) to match the other exactly.
An onsite question that extends basic island counting with a canonical form, similar in spirit to deduplicating repeated shapes in map imagery. The interviewer checks that your shape signature is truly translation-invariant and unambiguous.
What the Interviewer Expects
- Find each island with DFS or BFS
- Normalize cell coordinates relative to an anchor cell
- Store a hashable signature such as a sorted tuple of offsets
- Explain why a naive direction-string signature needs backtrack markers
- Discuss how to extend to rotations and reflections
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
- Treat rotations and reflections as the same shape.
- Return the most common shape and its count.
- How would you do this on a grid streamed row by row?
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 of 0/1.
- Islands are 4-connected.
- Same shape iff equal after translation.
- Output: count of distinct shapes....
High-Level Design
For each island, collect its cells with BFS starting from its first cell in row-major order. Subtract the minimum row and minimum column so the shape is anchored at the origin, sort the offsets, and c...