Number of Islands
Updated
Problem statement
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[]) → intExamples
Example 1
grid = ["11110","11010","11000","00000"]return = 1All land cells belong to one orthogonally connected component.
Example 2
grid = ["11000","11000","00100","00011"]return = 3The upper-left block, center cell, and lower-right pair are separate islands.
Example 3
grid = ["000","000"]return = 0The 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 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