Measure the Coastline Length of a Single Island on a Map Tile
Last updated: November 29, 2026
Quick Overview
A grid of 0s (water) and 1s (land) contains exactly one island with no lakes inside. Return the island's perimeter, counting each unit edge between land and water or the grid border.
0
0
0 solved
A grid of 0s (water) and 1s (land) contains exactly one island with no lakes inside. Return the island's perimeter, counting each unit edge between land and water or the grid border.
An Online Assessment style grid question themed on Google Maps tiles. It looks like a traversal problem, and the interviewer is checking whether you notice a counting shortcut, then whether you can also do it with DFS when the island is not the only land.
What the Interviewer Expects
- Notice each land cell contributes 4 minus its land neighbors
- Avoid double counting shared edges
- Offer a DFS version for when you must find the island first
- Handle land touching the grid border
- Give O(R*C) time
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
- What if the grid contains several islands and you want the perimeter of the largest?
- What if the island can contain lakes, and lake shore should not count?
- How would you compute this on a stream of rows without storing the whole grid?
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:
gridof 0 and 1, exactly one 4-connected island, no internal lakes. - Output: perimeter in unit edges.
- Grid border counts as water....
High-Level Design
Every land cell starts with 4 edges. Each pair of orthogonally adjacent land cells hides one shared edge from both cells, removing 2 from the total. So perimeter = where is...