← Bytedance Interview Insights
I've done number of islands before so I felt fine at first, then they said no in-place modification.
Use BFS to explore each unvisited land cell, marking all connected land cells as visited in a separate 2D boolean array. Iterate through every cell; when an unvisited '1' is found, increment the island count and launch BFS to mark its entire island.
Pro tip: Clarify upfront that you're using a visited matrix to avoid mutating the input, and mention that BFS is preferred over DFS here to avoid recursion depth issues on large grids.
Confirm grid dimensions, whether diagonal adjacency counts (usually no), and that the grid must remain unmodified. Discuss handling of empty grids or grids with no land.
Create a 2D boolean array of the same size as the grid, initially all false, and set island count to 0.
For each cell (i, j), if it's land ('1') and not visited, increment the island count and start a BFS from that cell.
Use a queue to explore all connected land cells (up, down, left, right). For each neighbor that is land and unvisited, mark it visited and enqueue it.
After processing all cells, return the total number of islands found.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.