Find Maximum Island Perimeter
Company: Snapchat
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates grid traversal and connected-component analysis skills, focusing on reasoning about perimeters and boundary conditions in binary matrices.
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