FastPrepNumber of Islands II
Problems/Number of Islands II

Number of Islands II

SpaceX logoSpaceX● HardFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a SpaceX phone screen. The report names the well-known exercise directly. Our version is about 99% the same as the reported problem.

Start with a rows by cols grid containing only water. Process each position in positions in order by turning that cell into land.

After every addition, append the current number of islands to the result. An island is a maximal group of land cells connected vertically or horizontally. Adding a cell that is already land changes nothing but still produces an output.

Function

numIslands2(rows: int, cols: int, positions: int[][]) → int[]

Examples

Example 1

rows = 3cols = 3positions = [[0,0],[0,1],[1,2],[2,1],[1,1]]return = [1,1,2,3,1]

The final center cell joins the three existing components into one island.

Example 2

rows = 1cols = 3positions = [[0,1],[0,1],[0,0],[0,2]]return = [1,1,1,1]

The repeated addition is a no-op, and both later cells attach to the existing island.

Example 3

rows = 2cols = 2positions = [[0,0],[1,1],[0,1],[1,0]]return = [1,2,1,1]

Diagonal cells are separate. Adding either remaining edge-adjacent cell merges them.

Constraints

  • 1 <= rows, cols <= 1000.
  • 1 <= positions.length <= 100000.
  • Every position is [row, col] with 0 <= row < rows and 0 <= col < cols.
  • Positions may repeat.

More SpaceX interview questions

  • Starlink Beam PlannerOA · PHONE SCREEN · Seen Nov 2026
  • Timestamped Telemetry Stream DifferencesPHONE SCREEN · Seen Sep 2026
  • Number of IslandsPHONE SCREEN · Seen Aug 2026
  • Button Combination DetectorPHONE SCREEN · Seen Feb 2026
  • Pressure Chamber Linear InterpolationPHONE SCREEN · Seen Jun 2025
  • Speed to Pressure LookupPHONE SCREEN · Seen Mar 2025
  • Simplified Time-Based Key-Value StorePHONE SCREEN · Seen Nov 2023
  • Rocket Component CostONSITE INTERVIEW · Seen Aug 2025
  • DNA Sequence MatchOA · Seen Apr 2025
  • Find the Duplicate NumberONSITE INTERVIEW · Seen Jan 2021

Similar questions at other companies

  • Count Islands in a Binary GridApple · PHONE SCREENApple interview questions
  • Number of Islands IIAmazon · ONSITE INTERVIEW
  • Dynamic Island CountsUber · PHONE SCREENUber interview questions
  • Shortest Distance from All BuildingsWaymo · PHONE SCREENWaymo interview questions
  • Group Linked Merchant RecordsStripe · PHONE SCREENStripe interview questions
  • Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Pacific Atlantic Water FlowAnduril · PHONE SCREENAnduril interview questions
  • Report Island Areas in Discovery OrderSnap Inc. · PHONE SCREENSnap Inc. interview questions
  • Number of IslandsWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
  • Maximum Damage PatchGecko Robotics · OA · PHONE SCREEN

Interview reports that mention this question

  • SpaceX Software Engineer OA and Interviews: 27 Experiences

SpaceX interview experiences

  • 2013–2026 SpaceX Full-Time OA / Interviews: 45 Experiences (Part 2 :)
  • SpaceX Full-Time Interview Cycle and Delayed Decision
  • SpaceX Experienced Engineer Full-Time Thirteen-Interview Process
  • SpaceX Senior Hardware Engineer Full-Time Interrupted Onsite
  • SpaceX Avionics Hardware Engineer Full-Time Four-Month Interview Process
All SpaceX interview questionsSee SpaceX hiring insights
Saved
class Solution {
    public int[] numIslands2(int rows, int cols, int[][] positions) {
        // Write your code here.
    }
}
Checking account…