Number of Islands
Updated
Problem statement
You are given a rectangular grid containing 0 for water and 1 for land.
An island is a maximal connected group of land cells. Connectivity uses only horizontal and vertical neighbors; diagonal contact does not connect islands.
Return the number of islands.
Function
numIslands(grid: int[][]) → intExamples
Example 1
grid = [[1,1,0],[0,1,0],[0,0,1]]return = 2Count connected components of land using only horizontal and vertical adjacency.
Example 2
grid = [[1,0],[0,1]]return = 2Count connected components of land using only horizontal and vertical adjacency.
Constraints
1 <= grid.length <= 5001 <= grid[0].length <= 500- Every row has the same length and every cell is
0or1.
More IMC Trading Phone Screen questions
- Election District Budget Allocation AnalysisPHONE SCREEN · Seen Aug 2026
- Uniform Variables: Smaller Below Larger SquaredPHONE SCREEN · Seen Mar 2026
- Combination Sum with Unlimited ReusePHONE SCREEN · Seen Dec 2025
- Valid Range of a Three-Outcome Probability ParameterPHONE SCREEN · Seen Dec 2025
- Expected Fishing CatchPHONE SCREEN · Seen Dec 2025
- Count Routes Through a City GridPHONE SCREEN · Seen Nov 2024
- Expected City Arrival and Return DaysPHONE SCREEN · Seen Nov 2024
- Stationary City Population SharesPHONE SCREEN · Seen Nov 2024
- Maximum Falling Path SumPHONE SCREEN · Seen Nov 2024
- Trading Profit Percentage ChangePHONE SCREEN · Seen Feb 2024
Similar questions at other companies
- Max Area of Island Without Changing the GridFlexport · PHONE SCREENFlexport interview questions
- Count Islands with Eight-Direction AdjacencyAmazon · PHONE SCREEN
- Count Islands in a Binary GridApple · PHONE SCREENApple interview questions
- Pacific Atlantic Water FlowAnduril · PHONE SCREENAnduril interview questions
- Detonate Bombs with Chain ReactionsAirbnb · PHONE SCREENAirbnb interview questions
- House Bounds in a Ragged GridRippling · OA · PHONE SCREENRippling interview questions
- Number of IslandsWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
- Maximum Damage PatchGecko Robotics · OA · PHONE SCREEN
- Count the IslandsAdobe · PHONE SCREENAdobe interview questions
- Maximum Area of IslandSkydio · PHONE SCREENSkydio interview questions