Measure the Coastline Length of a Single Island on a Map

Last updated: November 29, 2026

Quick Overview

A grid marks land with 1 and water with 0 and contains exactly one island with no lakes. Return the length of the island's perimeter.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


A grid marks land with 1 and water with 0 and contains exactly one island with no lakes. Return the length of the island's perimeter.

A Meta phone screen warm-up that can be framed as drawing an outline around a region in a Meta Maps style tile. It checks that the candidate prefers a simple counting identity over a DFS when no traversal is needed. Interviewers then extend it with lakes or multiple islands.

What the Interviewer Expects
  • Count four edges per land cell and subtract shared edges
  • Count each shared edge once by looking only right and down
  • Avoid an unnecessary DFS
  • Handle a single cell and full grids
  • Explain how lakes would change the answer
Key Topics to Cover
Grid traversal
Counting
Matrix
Geometry
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 island contained lakes and only the outer coastline counts?
  • What if there were several islands and you need each perimeter?
  • Can you compute the perimeter from a list of land cells without the 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
  • Grid up to 100 x 100 with exactly one four-connected island and no inner water.
  • Return the number of unit edges between land and water or the grid boundary....
High-Level Design

Each land cell contributes 4 unit edges. Whenever two land cells are adjacent, the edge between them is not coastline, and it was counted once by each cell, so subtract 2. Looking only to the right an...


Submit Your Answer
Markdown supported