Coding interview problems › Graphs & Grids

🗺️ Graphs & Grids Medium

Max Area of Island

Max Area of Island explained: flood-fill each island, count its cells and keep the largest. Tested Python BFS solution, pitfalls and a live grid visual.

Also searched as: largest island in a binary grid · biggest connected component size

🏝️

Try it live Free

Number of Islands & Grid BFS

Paint islands of different sizes in the Grid BFS lab and watch each flood: the number of cells it colours is that island's area.

Also useful: Graph Search: BFS vs DFS

Open the playground →

The problem

Given a grid of 0s (water) and 1s (land), an island is a group of 1s connected up, down, left or right. Return the area (number of cells) of the largest island, or 0 if there is no land.

Inputgrid = [[1,1,0,0],[1,0,0,1],[0,0,1,1]]
Output3

Two islands, of sizes 3 and 3.

The key insight

This is Number of Islands with one extra counter. Every time the scan finds unseen land, flood the whole island exactly as before, but count the cells you pop. Compare that size with the best so far.

Brute force first: Starting a fresh flood from every land cell (without a shared visited set) finds the right answer but repeats the same island many times, O((m·n)²).

Step-by-step approach

  1. Scan every cell; skip water and cells already visited.
  2. On unseen land, mark it visited, push it on a queue and set area = 0.
  3. Pop cells, increment area, and push unvisited land neighbours (marking them when pushed).
  4. When the queue empties, update best = max(best, area).
  5. Return best.

Python solution

PYTHON 3tested
from collections import deque


def max_area_of_island(grid):
    rows, cols = len(grid), len(grid[0]) if grid else 0
    seen = set()
    best = 0
    for r in range(rows):
        for c in range(cols):
            if grid[r][c] != 1 or (r, c) in seen:
                continue
            seen.add((r, c))
            q, area = deque([(r, c)]), 0
            while q:
                x, y = q.popleft()
                area += 1
                for nx, ny in ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)):
                    if (0 <= nx < rows and 0 <= ny < cols
                            and grid[nx][ny] == 1 and (nx, ny) not in seen):
                        seen.add((nx, ny))
                        q.append((nx, ny))
            best = max(best, area)
    return best

Complexity

TimeO(m·n)
SpaceO(m·n)

Each cell is visited once across all floods, so total work is proportional to the number of cells. The visited set can hold every cell.

Common mistakes

Frequently asked questions

What is the time and space complexity of Max Area of Island?

O(m·n) time because every cell is processed once, and O(m·n) space in the worst case for the visited set and the queue.

Can I use recursive DFS?

Yes, return 1 plus the areas of the four neighbours. It is shorter, but a large all-land grid can exceed Python's recursion limit, so BFS or an explicit stack is safer.

What is the Making a Large Island follow-up?

You may flip one 0 to 1. Label every island with an id and size first, then for each 0 add up the sizes of the distinct neighbouring island ids plus one.

Preparing for placements?

Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.

All coding interview problems More Graphs & Grids Graph Search: BFS vs DFS lab Placement prep guides

Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.