Prepin
Log in

Determine the maximum perimeter of an island using a two-dimensional array where 1’s represent land and 0’s represent water.

Approach / Explanation

This problem presents a grid composed of 1s and 0s, where 1s denote land and 0s indicate water. This grid serves as a top-down representation of a group of islands. Your objective is to identify the island that has the greatest perimeter. The perimeter of an island is determined by the count of direct boundaries between water and land. To approach this problem, you will need to explore the entire grid. For each cell that represents land (1), examine its four neighboring cells. If a neighbor is either at the edge of the grid or represents water, increment the perimeter by 1. During your exploration, keep a record of the maximum perimeter encountered. Important aspects to keep in mind: Grasping the 2D array depiction of islands and water, as well as how to compute the perimeter. Being familiar with grid traversal techniques and checking neighboring cells. Implementing a maximum variable to track the largest perimeter found.

Suggested Answer

Let's examine a straightforward two-dimensional grid: grid = [[0, 1, 0, 0], [1, 1, 1, 0], [0, 1, 0, 0], [1, 1, 0, 0]] Go through each cell in the grid. If a cell contains land (1), check its neighboring cells: For cells located on the edge of the grid, increase the perimeter count by 1. For cells located internally, if any adjacent cell is water, increase the perimeter by 1. During this process, keep track of the maximum perimeter found. In this example, the largest island (composed of 1s) has a perimeter of 10.

Alternative Answer

Here’s another example: grid = [[0, 1, 0, 0], [1, 1, 1, 0], [1, 1, 1, 1], [0, 0, 1, 0], [1, 1, 1, 1]] By applying the same method as mentioned earlier, the largest island in this grid has a perimeter of 16.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Meta (Facebook)

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.