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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

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
grid
counting
DFS
perimeter
neighbor analysis
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
  • 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 Problems
Sample Answer
Requirements
  • Input: grid of 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 = 4⋅L−2⋅S4 \cdot L - 2 \cdot S where LL is...


Submit Your Answer
Markdown supported