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.
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.
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
Pro subscription, billed yearly — or $19 month-to-month. Cancel anytime.
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
Pro subscription, billed yearly — or $19 month-to-month. Cancel anytime.