Count the Islands
Updated
Problem statement
You are given a grid of m rows and n columns. A cell with 1 is land and a cell with 0 is water. Two land cells belong to the same island when you can walk from one to the other through land cells, each step going up, down, left or right (not diagonally). Everything outside the grid is water.
Return the number of islands.
Function
countIslands(grid: int[][]) → intExamples
Example 1
grid = [[1,1,0,0],[0,1,0,1],[1,0,0,1]]return = 3The cells (0,0), (0,1) and (1,1) touch side by side and form one island. (1,3) and (2,3) form a second. (2,0) touches (1,1) only at a corner, which does not count, so it is a third island. The answer is 3.
Example 2
grid = [[0,0],[0,0]]return = 0There is no land, so there are no islands.
Example 3
grid = [[1,0,1,0,1]]return = 3Three single cells of land separated by water: 3 islands.
Constraints
1 <= m, n <= 100, wheremis the number of rows andnthe number of columns- every row of
gridhasncells grid[r][c]is0or1
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Count Islands in a Binary GridApple · PHONE SCREENApple interview questions
- Number of IslandsExpedia · ONSITE INTERVIEWExpedia interview questions
- Max Area of Island Without Changing the GridFlexport · PHONE SCREENFlexport interview questions
- Count Islands with Eight-Direction AdjacencyAmazon · PHONE SCREEN
- Pacific Atlantic Water FlowAnduril · PHONE SCREENAnduril interview questions
- House Bounds in a Ragged GridRippling · OA · PHONE SCREENRippling interview questions
- Maximum Damage PatchGecko Robotics · OA · PHONE SCREEN
- Maximum Area of IslandSkydio · PHONE SCREENSkydio interview questions
- Number of Islands with Diagonal ConnectivityGoogle · ONSITE INTERVIEWGoogle interview questions
- Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions