Spent like two minutes second-guessing whether to go BFS or DFS and probably looked indecisive.
Treat the grid as a graph and use DFS or BFS to traverse each unvisited land cell, marking all connected land cells as visited to count one island. Iterate through every cell, and when an unvisited '1' is found, increment the island count and explore its entire connected component.
Pro tip: Mention that you can modify the grid in-place to mark visited cells (e.g., changing '1' to '0') to save space, but clarify that this mutates the input; if mutation is not allowed, use a separate visited set or matrix. Also, discuss handling edge cases like empty grid or all water.
Confirm that islands are defined by 4-directional connectivity and that grid borders are water. Ask about grid size limits, whether the input can be modified, and if recursion depth is a concern.
Decide between DFS (recursive or iterative) and BFS. DFS is simpler but may risk stack overflow for large grids; BFS uses a queue and avoids recursion limits.
For each unvisited land cell, start a traversal that marks all connected land cells as visited. Use a visited set/matrix or modify the grid in-place (e.g., set to '0').
Increment the island count each time you start a traversal from an unvisited land cell. Ensure boundary checks prevent out-of-bounds access.
State time complexity O(M*N) and space complexity O(M*N) for visited set or O(min(M,N)) for BFS queue in worst case. Walk through a small example and edge cases.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.