Count Distinct Shapes of Land Clusters on a Map Grid

Last updated: November 29, 2026

Quick Overview

Given a binary grid where 1 is land, count how many distinct island shapes exist, where two islands have the same shape if one can be translated (not rotated or reflected) to match the other.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given a binary grid where 1 is land, count how many distinct island shapes exist, where two islands have the same shape if one can be translated (not rotated or reflected) to match the other.

A shape-hashing grid traversal in the style of Meta's onsite coding interviews, loosely tied to deduplicating repeated building footprints in map tiles. The interviewer checks that you find each island once and build a translation-invariant canonical signature.

What the Interviewer Expects
  • Finds each island with BFS or DFS
  • Normalizes cell coordinates relative to a fixed origin cell
  • Stores a canonical tuple or frozenset in a set
  • Explains why a raw traversal path string needs backtrack markers
  • Discusses extending to rotations and reflections
Key Topics to Cover
DFS
Hashing
Grids
Canonical form
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 treat rotations and reflections as the same shape?
  • How would you return the most common shape?
  • What if the grid streams in row by row?
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 of 0s and 1s, up to 50 x 50. Output: number of distinct shapes under translation only. Connectivity is 4-directional....

High-Level Design

Scan the grid in row-major order. When an unvisited land cell appears, it is the top-left-most cell of its island in scan order; call it the origin. Traverse the island and record each cell as `(r - r...


Submit Your Answer
Markdown supported