Quick Overview

Find the largest four-neighbor island area in a binary matrix, including the component-size challenges of row-by-row streaming.

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

  1. Only cells sharing an edge can belong to the same island.
  2. An empty matrix or one without land returns 0.

Loading coding console...

Show the approach

Approach

Scan the complete matrix. For each unseen land cell, traverse all four-neighbor land cells in its connected component and count the cells reached. Mark each neighbor before pushing it so no cell is counted twice. The largest component count is the maximum island area. If there is no land, the initial maximum zero is returned.

Time complexity:
O(R × C), for R rows and C columns.
Space complexity:
O(R × C) for visited cells and the traversal stack.