FastPrepNumber of Islands II
Problems/Number of Islands II

Number of Islands II

Amazon logoAmazon● HardONSITE INTERVIEW

Updated Sep 2026

QuestionFunctionExamples

Problem statement

Source 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.

An island is a maximal group of land cells connected vertically or horizontally. Diagonal cells are not connected.

Interview follow-up

The rest of the discussion covered the DSU approach, how union-find works, edge cases, and the time and space complexity.

Function

numIslands2(m: int, n: int, positions: int[][]) → int[]

Examples

Example 1

m = 3n = 3positions = [[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.

Example 2

m = 2n = 2positions = [[0,0],[1,1],[0,1]]return = [1,2,1]

The third addition connects the two existing islands into one.

More Amazon Onsite Interview questions

  • Assign Escalations to TeamsONSITE INTERVIEW · Seen Nov 2026
  • Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026
  • Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Maximum Earnings Across WarehousesONSITE INTERVIEW · Seen Nov 2026
  • Priority Message BufferONSITE INTERVIEW · Seen Nov 2026
  • Search in a Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Maximal SquareONSITE INTERVIEW · Seen Nov 2026
  • Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Number of Islands IISpaceX · PHONE SCREEN
  • Count Islands in a Binary GridApple · PHONE SCREEN
  • Find All People With SecretIDFC Bank · ONSITE INTERVIEW
  • Largest Island After One Land ConversionGoogle · ONSITE INTERVIEW
  • Making a Large IslandMeta · ONSITE INTERVIEW
  • Number of IslandsFleetx · ONSITE INTERVIEW
  • Number of IslandsIntuit · ONSITE INTERVIEW
  • Island Count and Maximum AreaOracle · ONSITE INTERVIEW
  • Radar Barrier CrossingSnap Inc. · ONSITE INTERVIEW
  • Shortest Increasing Path to a TargetUber · ONSITE INTERVIEW

Amazon interview experiences

  • Amazon Experienced SDE Onsite Questions: 1 Experience
  • Amazon SDE Onsite Interview Questions: 9 Experiences
  • Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
  • Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
  • Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
  • Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)
See Amazon hiring insights
Saved
class Solution {
    public int[] numIslands2(int m, int n, int[][] positions) {
      // write your code here
    }
}
Checking account…