DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
medium
Graph

Number of Islands

**Problem Statement:** Given the required input arguments, write an efficient algorithm to solve the **Number of Islands** problem. Implement the required logic as specified by standard definitions for this classic algorithmic challenge. **Hint / Expected Approach:** DFS/BFS flood fill from each unvisited '1' **Edge Cases to Consider:** - (1) All water grid - (2) All land grid - (3) Single cell grid

Examples

Example 1
Input: [["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]
Output: 1
Explanation: Single large island
Example 2
Input: [["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]
Output: 3
Explanation: Three separate islands
Example 3
Input: [["0"]]
Output: 0
Explanation: All water

Constraints

  • ▪2 <= vertices <= 1000
  • ▪0 <= edges <= 5000
  • ▪Avoid infinite cycles during depth traversal

Watch Out For Edge Cases

  • ▪All water grid
  • ▪All land grid
  • ▪Single cell grid
Frequently Asked At
AmazonGoogleMicrosoftMetaAdobeFlipkartZomato