Went with DFS pretty much immediately, which felt right.
Treat the grid as a graph where each land cell is a node connected to its adjacent land cells. Use DFS or BFS to explore and mark each connected component, incrementing a counter for each new component found. This efficiently counts distinct islands in O(m*n) time.
Pro tip: Mention that you can optimize space by modifying the grid in-place to mark visited cells (e.g., changing '1' to '0'), but clarify that if the input must be preserved, a separate visited matrix or union-find is needed. Also, discuss handling edge cases like empty grid or all water.
Confirm that islands are formed by horizontally or vertically adjacent land cells, and that diagonal connections do not count. Ask about grid size constraints and whether the grid can be modified.
Select either DFS, BFS, or Union-Find. DFS/BFS are simpler and efficient for this problem; Union-Find is an alternative if you prefer iterative approaches or need to handle dynamic updates.
Iterate through each cell. When a land cell is found, increment the island count and perform DFS/BFS to mark all connected land cells as visited (e.g., set to '0' or use a visited set).
State that time complexity is O(m*n) since each cell is visited once, and space complexity is O(m*n) in the worst case for the recursion stack or queue, but can be optimized to O(min(m,n)) with BFS if using a queue.
Mention testing with an empty grid, a grid with no land, a single island, multiple disconnected islands, and a grid where all cells are land (one island).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.