Went with DFS, marked visited cells in-place by flipping 1s to 0s.
Use a graph traversal algorithm like BFS or DFS to explore each unvisited land cell and mark all connected land cells as visited, incrementing the island count for each traversal. Alternatively, use Union-Find to group connected land cells and count distinct sets. Discuss time and space complexity, and consider edge cases.
Pro tip: Mention that you can optimize space by modifying the grid in-place (e.g., changing '1' to '0') if allowed, but clarify with the interviewer first. Also, be prepared to discuss trade-offs between BFS, DFS, and Union-Find in terms of performance and code complexity.
Confirm the definition of an island, connectivity (4-directional), and input constraints (grid size, mutability). Ask if the grid can be modified.
Select a traversal method (BFS/DFS) or Union-Find. Explain why it's suitable and outline the approach.
Write clean code with helper functions. For BFS/DFS, iterate through each cell; if it's land and unvisited, increment count and traverse to mark all connected land.
State time complexity O(M*N) and space complexity O(M*N) for BFS/DFS (worst-case queue/stack) or O(M*N) for Union-Find. Mention in-place modification reduces space to O(1) extra if allowed.
Walk through a small example, including edge cases like empty grid, all water, all land, and single row/column.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.