Stack Bash - Master data structures and algorithms

Number of islands

Medium
You're given a m x n 2D grid of 1s and 0s, where 1 represents land and 0 represents water, find the number of islands that exist in the grid.
Consider an island as a group of adjacent 1s.
Adjancent cells are cells that are in either 4 directions - left, right, down, or up.
For example, given the following grid
grid = [
[1, 0, 0],
[0, 1, 1],
[0, 1, 0]
]
Your function should return 2.

Try it

Before you look at the solution, try to code it yourself.

Solution

9 Essential Trees & Graphs Coding Interview Problems

Master Trees & Graphs by trying the coding challenges below.
  1. 1.Inorder TraversalEasy
  2. 2.Tree SumEasy
  3. 3.Tree HeightEasy
  4. 4.LCAMedium
  5. 5.Max Path SumHard
  6. 6.Search mazeMedium
  7. 7.Number of islandsMedium
  8. 8.Kth SmallestMedium
  9. 9.Sort K ArraysHard

Want to confidently pass your next coding interview?

Stack Bash helps new and veteran software engineers master data structures and algorithms for technical interviews.