← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Google SWE coding round, one question on grid traversal. Pretty standard stuff but I fumbled the edge cases more than I'd like to admit.

Questions Asked (1)

Q1

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

Algorithms & Data Structures
Author's notes

I went straight to BFS which felt right, but spent too long second-guessing whether to use DFS instead.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Treat the grid as a graph and use DFS or BFS to explore each unvisited land cell, marking all connected land cells as visited. Count the number of times you initiate a traversal from an unvisited land cell. Alternatively, use Union-Find to merge adjacent land cells and count distinct sets.

Pro tip: Clarify edge cases upfront (empty grid, all water, all land) and mention that you can mutate the grid to save space, but ask if that's acceptable. Also, discuss time and space complexity trade-offs between DFS/BFS and Union-Find.

1. Clarify the problem

Confirm the definition of an island, adjacency rules (4-directional), and input constraints (grid size, mutability). Ask about edge cases like empty grid or no land.

2. Choose an algorithm

Decide between DFS/BFS (simpler, O(mn) time, O(mn) space worst-case) and Union-Find (good for dynamic connectivity, O(mn α) time). Explain your choice based on constraints.

3. Outline the traversal

Iterate through each cell; when you find an unvisited '1', increment island count and launch a traversal (DFS/BFS) to mark all connected '1's as visited (e.g., set to '0' or use a visited set).

4. Analyze complexity

State time complexity O(mn) since each cell is visited once, and space complexity O(mn) for recursion stack or queue in worst case (all land). Mention that Union-Find uses O(mn) space for parent array.

5. Test with examples

Walk through a small example (e.g., 3x3 with one island) and edge cases (empty grid, single row/column, all water, all land) to verify correctness.

Key Points to Mention

  • Graph traversal (DFS/BFS) or Union-Find as core approaches
  • Time complexity O(mn) and space complexity O(mn) worst-case
  • Mutating the grid to mark visited cells (if allowed) to save space
  • Handling edge cases: empty grid, all water, all land, single row/column
  • Avoiding revisiting cells by marking them as visited
  • Potential follow-up: number of distinct islands or largest island

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