Count Islands in a Grid
Company: Generalmotors
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates skills in graph traversal and grid connectivity detection, testing competency in reasoning about adjacency, state management, and edge-case handling such as empty grids and the exclusion of diagonal connections.
Constraints
- grid contains 0 water and 1 land
Examples
Input: ([[1, 1, 0], [0, 1, 0], [1, 0, 1]],)
Expected Output: 3
Explanation: Diagonal land is not connected.
Input: ([],)
Expected Output: 0
Explanation: Empty grid.
Input: ([[0, 0]],)
Expected Output: 0
Explanation: No land.
Input: ([[1, 1], [1, 1]],)
Expected Output: 1
Explanation: One island.
Hints
- BFS or DFS from each unvisited land cell.