Find the Maximum Island Area
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Given a two-dimensional binary matrix, return the maximum area of any island. An island is a connected component of cells containing `1`, using only up, down, left, and right adjacency. Its area is the number of cells in that component. Diagonal contact does not connect islands.
### Input and Output
- Input: a rectangular matrix containing only `0` and `1`.
- Output: one integer, the largest island area.
- For this practice version, an empty matrix or a matrix with no land has answer `0`.
### Example
```text
grid = [
[1, 1, 0],
[0, 1, 0],
[1, 0, 1]
]
answer = 3
```
The upper-left island has three cells, which is larger than either one-cell island in the last row.
### Discussion Extension
How would you maintain the maximum island area if matrix rows arrived one at a time? The source asks about row streaming but supplies no streaming-call interface; the function above receives the complete matrix.
Overview: Find the largest four-neighbor island area in a binary matrix, including the component-size challenges of row-by-row streaming.
Read the full Amazon Software Engineer interview experience this question came from
Given a two-dimensional binary matrix, return the maximum area of any island. An island is a connected component of cells containing `1`, using only up, down, left, and right adjacency. Its area is the number of cells in that component. Diagonal contact does not connect islands.
### Input and Output
- Input: a rectangular matrix containing only `0` and `1`.
- Output: one integer, the largest island area.
- For this practice version, an empty matrix or a matrix with no land has answer `0`.
### Example
```text
grid = [
[1, 1, 0],
[0, 1, 0],
[1, 0, 1]
]
answer = 3
```
The upper-left island has three cells, which is larger than either one-cell island in the last row.
### Discussion Extension
How would you maintain the maximum island area if matrix rows arrived one at a time? The source asks about row streaming but supplies no streaming-call interface; the function above receives the complete matrix.
Constraints
- grid is a finite rectangular matrix containing only integer 0 and 1; an empty matrix is allowed.
- Only up, down, left, and right adjacency connects land cells.
- No numeric row or column bound is supplied.
Examples
Input: ([[1, 1, 0], [0, 1, 0], [1, 0, 1]],)
Expected Output: 3
Explanation: The upper-left component has area three, larger than the isolated cells.
Input: ([[1, 0], [0, 1]],)
Expected Output: 1
Explanation: Diagonal land cells each have area one.
Hints
- Only cells sharing an edge can belong to the same island.
- An empty matrix or one without land returns 0.