← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
May 2026Remote

Summary

Google SWE coding round, pretty much what you'd expect. One grid problem, some follow-ups, and a lot of time spent hoping I remembered my graph traversal.

Questions Asked (1)

Q1

Given a 2D binary grid of '1's (land) and '0's (water), count the number of islands, where an island is a group of horizontally or vertically connected land cells.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I went with DFS pretty much immediately.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a graph traversal algorithm (DFS or BFS) to explore each unvisited land cell and mark all connected land cells as visited, incrementing the island count for each new traversal. Alternatively, use Union-Find to group connected land cells and count distinct sets. Discuss trade-offs between approaches.

Pro tip: Clarify whether you can modify the input grid; if not, use a separate visited matrix or Union-Find to avoid mutating data. Also, mention that BFS avoids recursion depth issues for large grids.

1. Clarify problem constraints and edge cases

Ask about grid dimensions, whether the grid can be modified, and if diagonal connections count. Confirm that only horizontal and vertical connections define an island.

2. Choose an algorithm

Select between DFS, BFS, or Union-Find based on constraints. For most interviews, DFS is simplest; BFS is safer for large grids; Union-Find is efficient for dynamic connectivity.

3. Implement traversal and marking

Iterate through each cell; when a '1' is found, increment island count and perform traversal to mark all connected '1's as visited (e.g., set to '0' or use a visited set).

4. Analyze complexity and optimize

State time complexity O(M×N) and space complexity O(M×N) for visited matrix or O(min(M,N)) for BFS queue. Discuss potential optimizations like early termination or iterative deepening.

5. Test with examples

Walk through a small grid (e.g., 3x3) to verify correctness, including edge cases like all water, all land, and single row/column.

Key Points to Mention

  • Time and space complexity analysis for each approach
  • Handling of edge cases: empty grid, all water, all land, non-rectangular grid
  • Choice of traversal: DFS (recursive/iterative), BFS, or Union-Find
  • Avoiding stack overflow with iterative DFS or BFS for large grids
  • In-place modification vs. using a visited matrix
  • Potential follow-up: number of distinct island shapes or maximum island area

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.