← Google Interview Insights

Google·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
May 2026

Summary

Google SWE coding round, basically one problem but they squeezed two versions of it out of me. The static grid version was fine, the streaming follow-up is where things got real.

Questions Asked (2)

Q1

Given a binary grid, count the number of islands where connectivity is 4-directional (up, down, left, right).

Algorithms & Data Structures
Author's notes

I went with DFS immediately and it was fine.

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. Each time you start a traversal from an unvisited land cell, increment the island count. This approach runs in O(m*n) time and space.

Pro tip: Clarify edge cases upfront (empty grid, all water, all land) and mention that you can optimize space by mutating the grid in-place (e.g., setting visited cells to '0') if allowed, but always ask before modifying input.

1. Clarify the problem

Confirm the definition of an island, connectivity (4-directional), and input format (e.g., '1' for land, '0' for water). Ask about edge cases like empty grid or large dimensions.

2. Choose traversal method

Decide between DFS (recursive or iterative) and BFS. Discuss trade-offs: DFS is simpler but may cause stack overflow on large grids; BFS uses a queue and avoids recursion depth issues.

3. Implement traversal and counting

Iterate through each cell. When you find an unvisited land cell, increment the island count and launch a traversal to mark all connected land cells as visited.

4. Analyze complexity

State that time complexity is O(m*n) because each cell is visited once. Space complexity is O(m*n) in the worst case for the recursion stack or queue.

5. Test with examples

Walk through a small example (e.g., 3x3 grid) to verify correctness. Mention potential pitfalls like diagonal connections or out-of-bounds checks.

Key Points to Mention

  • Use DFS or BFS to explore connected components.
  • Mark visited cells to avoid infinite loops (e.g., change '1' to '0' or use a visited set).
  • Handle edge cases: empty grid, no land, all land.
  • Time and space complexity analysis: O(m*n) time, O(m*n) space worst-case.
  • Iterative BFS avoids recursion depth limits for large grids.
  • In-place modification can reduce space but may not be allowed; ask first.

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

Q2

Now imagine land cells arrive one at a time in a stream. After each addition, return the current island count.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is the one I was not ready for at the speed they wanted.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the grid with a Union-Find (Disjoint Set Union) data structure to dynamically track connected components as land cells are added. For each new land cell, increment the island count, then union it with any adjacent existing land cells, decrementing the count for each successful union. This yields O(α(n)) amortized time per addition, where α is the inverse Ackermann function.

Pro tip: Mention that Union-Find with path compression and union by rank is the optimal solution, and briefly discuss how you would handle edge cases like duplicate land additions or out-of-bounds coordinates. This shows you consider robustness and real-world constraints.

1. Clarify the problem and constraints

Ask about grid size, whether coordinates are guaranteed unique, and if the stream can contain duplicate or invalid cells. Confirm that islands are 4-directionally connected.

2. Choose the right data structure

Select Union-Find (Disjoint Set Union) to efficiently manage connected components. Explain why a simple BFS/DFS per addition would be too slow (O(N) per query).

3. Design the algorithm

For each new land cell: increment island count, mark it as land, then check its four neighbors. For each neighbor that is land, union the two cells; if they were in different sets, decrement the island count.

4. Analyze complexity and trade-offs

State that each addition takes O(α(N)) amortized time with path compression and union by rank, where N is the number of cells. Space is O(N) for the parent and rank arrays.

5. Handle edge cases and test

Discuss duplicate additions (ignore or handle gracefully), out-of-bounds coordinates, and the initial empty grid. Walk through a small example to verify correctness.

Key Points to Mention

  • Union-Find (Disjoint Set Union) with path compression and union by rank for near-constant time operations.
  • Increment island count for each new land cell, then decrement for each successful union with an adjacent land cell.
  • Time complexity: O(α(N)) amortized per addition, where α is the inverse Ackermann function.
  • Space complexity: O(N) for the Union-Find data structure, where N is the total number of cells.
  • Edge cases: duplicate land additions, out-of-bounds coordinates, and 4-directional connectivity.
  • Alternative approaches (e.g., BFS/DFS per addition) and why they are less efficient.

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