Count Distinct Delivery Zone Shapes on a City Grid
Last updated: November 29, 2026
Quick Overview
A city map is a binary grid where 1 marks a block inside a delivery zone, and connected 1s (four-directionally) form one zone. Return how many distinct zone shapes exist, where two zones have the same shape if one can be translated onto the other without rotation or reflection.
Amazon
November 29, 20260
0
0 solved
A city map is a binary grid where 1 marks a block inside a delivery zone, and connected 1s (four-directionally) form one zone. Return how many distinct zone shapes exist, where two zones have the same shape if one can be translated onto the other without rotation or reflection.
A step up from counting islands, framed around Amazon last-mile zone planning. The interviewer is checking that you can produce a canonical, translation-invariant signature for each component and that you know why a naive DFS path string can collide.
What the Interviewer Expects
- Find components with DFS or BFS
- Normalize each component relative to its first cell
- Store a canonical signature in a set
- Explain why traversal-order strings need backtrack markers
- Discuss the rotation and reflection variant
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
- How would you treat rotations and reflections as the same shape?
- How would you also report the count of zones per shape?
- What changes for an eight-directional adjacency?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Grid of 0/1, may be empty. Shape equality is translation only. Return an int count of distinct shapes....
High-Level Design
Scan row by row. When we hit an unvisited 1, flood fill it iteratively, collecting cell coordinates as offsets (r - r0, c - c0) from the starting cell. Because we always start at the topmost-leftmos...