PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates grid traversal and connected-component analysis skills, focusing on reasoning about perimeters and boundary conditions in binary matrices.

  • medium
  • Snapchat
  • Coding & Algorithms
  • Software Engineer

Find Maximum Island Perimeter

Company: Snapchat

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given an `m x n` binary grid where `1` represents land and `0` represents water. An island is a group of land cells connected vertically or horizontally. For each island, define its **perimeter** as the number of land edges that touch either water or the boundary of the grid. Return the **maximum perimeter** among all islands in the grid. If the grid contains no land, return `0`. You may solve this using either DFS or BFS. Example: - Input: ``` [ [1,1,0,0], [1,0,0,1], [0,0,1,1] ] ``` - Output: `8` Because the island with the largest perimeter has perimeter 8.

Quick Answer: This question evaluates grid traversal and connected-component analysis skills, focusing on reasoning about perimeters and boundary conditions in binary matrices.

You are given an m x n binary grid where 1 represents land and 0 represents water. An island is a group of land cells connected vertically or horizontally. The perimeter of an island is the total number of land edges that touch either water or the boundary of the grid. Return the maximum perimeter among all islands in the grid. If the grid contains no land, return 0. You may solve this problem using either DFS or BFS.

Constraints

  • 0 <= m, n <= 200
  • grid[i][j] is either 0 or 1
  • Cells are connected only vertically and horizontally

Examples

Input: ([[1,1,0,0],[1,0,0,1],[0,0,1,1]],)

Expected Output: 8

Explanation: There are two islands, and both have perimeter 8, so the maximum perimeter is 8.

Input: ([[0,0],[0,0]],)

Expected Output: 0

Explanation: There is no land in the grid, so the answer is 0.

Input: ([],)

Expected Output: 0

Explanation: An empty grid contains no islands.

Input: ([[1]],)

Expected Output: 4

Explanation: A single land cell has all 4 sides exposed.

Input: ([[1,1,1],[1,0,1],[1,1,1]],)

Expected Output: 16

Explanation: The island surrounds a water cell. The outer boundary contributes 12 and the inner hole contributes 4, for a total of 16.

Input: ([[1,0,1,1],[1,0,1,0],[1,0,1,0]],)

Expected Output: 10

Explanation: There are two islands. The left vertical island has perimeter 8, while the right island has perimeter 10, so the maximum is 10.

Hints

  1. Traverse each unvisited land cell with DFS or BFS and compute the perimeter of that connected component.
  2. For every land cell, check its 4 neighbors. If a neighbor is out of bounds or water, that side contributes 1 to the perimeter.
Last updated: May 7, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Determine Whether Courses Can Be Completed - Snapchat (medium)
  • Solve Decimal Coin Change - Snapchat (medium)
  • Solve Three Algorithmic Tasks - Snapchat (hard)
  • Implement a Timestamped Counter - Snapchat (medium)
  • Implement a custom list with iterator and map - Snapchat (medium)