Problem · Array

Number of Islands II

HardAmazonONSITE INTERVIEW
See Amazon hiring insights

A quick note: this problem is backed by a real Amazon onsite interview report. The report specified an all-water grid, one land addition at a time, four-directional merging, and an island count after every update. It did not include the exact interface, examples, constraints, or duplicate-update behavior. The core task match is about 95%; the judged positions are distinct.

Start with an m by n grid containing only water. For each distinct position [row, col] in positions, turn that cell into land and append the current number of islands to the result.

The problem statement continues
Pro
Examples
01 · Example 1
m = 3
n = 3
positions = [[0,0],[0,1],[1,2],[2,1]]
return = [1,1,2,3]

The second cell joins the first island. The final two additions are not four-directionally adjacent to existing land.

Original screenshot from the interview report
Pro
FastPrep Pro
Reported in 1 Amazon interview this week

Unlock the full problem

  • Full problem statement and constraints
  • 1 more worked example, explained
  • Guided hints and editorial
  • Run your code on real test cases
$9/month

Pro subscription, billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week
CodePython 3
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 Amazon interview this week

Unlock the full problem

  • Full problem statement and constraints
  • 1 more worked example, explained
  • Guided hints and editorial
  • Run your code on real test cases
$9/month

Pro subscription, billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week