FastPrepNumber of Islands
Problems/Number of Islands

Number of Islands

SpaceX logoSpaceX● MediumFULLTIMEPHONE 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.

Given an array of equal-length strings grid, where '1' represents land and '0' represents water, return the number of islands.

An island is a maximal group of land cells connected horizontally or vertically. Cells outside the matrix are water.

Function

numIslands(grid: String[]) → int

Examples

Example 1

grid = ["11110","11010","11000","00000"]return = 1
grid
01234011110111010211000300000

All land cells belong to one orthogonally connected component.

Example 2

grid = ["11000","11000","00100","00011"]return = 3
grid
01234011000111000200100300011

The upper-left block, center cell, and lower-right pair are separate islands.

Example 3

grid = ["000","000"]return = 0
grid
01200001000

The grid contains no land cells.

Constraints

  • 1 <= grid.length <= 300.
  • 1 <= grid[i].length <= 300, and every row has the same length.
  • Every character is '0' or '1'.

More SpaceX interview questions

  • Starlink Beam PlannerOA · PHONE SCREEN · Seen Nov 2026
  • Timestamped Telemetry Stream DifferencesPHONE SCREEN · Seen Sep 2026
  • Number of Islands IIPHONE 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

  • Number of IslandsAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Number of IslandsGoogle · PHONE SCREEN · ONSITE INTERVIEW
  • Count Connected IslandsUber · PHONE SCREEN
  • Count Islands in a Binary GridApple · PHONE SCREEN
  • Pacific Atlantic Water FlowAnduril · PHONE SCREEN
  • Grid Pathfinding with Obstacles (DFS)Roblox · PHONE SCREEN · ONSITE INTERVIEW
  • Number of IslandsGoldman Sachs · OA · PHONE SCREEN · ONSITE INTERVIEW
  • Number of IslandsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEW
  • Maximum Damage PatchGecko Robotics · OA · PHONE SCREEN
  • Maximum Area of IslandSkydio · 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
See SpaceX hiring insights
Saved
class Solution {
    public int numIslands(String[] grid) {
        // Write your code here.
    }
}
Checking account…