← Google Interview Insights

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

IntermediatePrefer not to say
Jul 2026

Summary

Google SWE coding round centered on multi-source BFS, specifically the rotten oranges / taxi distance variant. The follow-up about moving sources was the part that really separated candidates, and I don't think I handled it as cleanly as I wanted.

Questions Asked (2)

Q1

Given a grid where some cells are 'sources' (rotten oranges, taxis, or gates), compute the distance from every other cell to the nearest source.

Algorithms & Data Structures
Author's notes

Classic multi-source BFS setup.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use multi-source BFS starting from all source cells simultaneously, treating the grid as an unweighted graph. Initialize a queue with all sources, set their distance to 0, and perform BFS to propagate distances to all reachable cells. For unreachable cells, leave distance as infinity or -1.

Pro tip: Clarify upfront that you assume 4-directional movement and that sources have distance 0; this shows attention to problem constraints and avoids ambiguity. Also, mention that if the grid is large, you can optimize space by using a 2D array for distances and a queue for BFS, but avoid recursion due to stack overflow.

1. Clarify problem and constraints

Ask about movement directions (4 or 8), whether diagonal moves are allowed, and what to return for unreachable cells. Confirm that sources are given and distances are Manhattan if only 4-directional.

2. Choose algorithm: multi-source BFS

Explain that BFS from all sources simultaneously computes shortest distances in O(rows*cols) time. Contrast with running BFS from each empty cell, which would be O((rows*cols)^2).

3. Initialize data structures

Create a distance matrix initialized to infinity (or -1) and a queue. Enqueue all source cells and set their distance to 0.

4. Perform BFS propagation

While queue is not empty, dequeue a cell, explore its valid neighbors (within bounds, not walls, and not yet visited), update their distance as current distance + 1, and enqueue them.

5. Return result and analyze complexity

After BFS, the distance matrix holds the shortest distance from each cell to the nearest source. State time complexity O(rows*cols) and space complexity O(rows*cols) for the queue and distance matrix.

Key Points to Mention

  • Multi-source BFS treats all sources as starting points at distance 0, ensuring the first time a cell is reached is via the shortest path.
  • Time complexity is O(rows * cols) because each cell is enqueued and dequeued at most once.
  • Space complexity is O(rows * cols) for the distance matrix and queue in the worst case.
  • Handle edge cases: no sources (all distances remain infinity), sources covering all cells, and obstacles/walls that block movement.
  • If diagonal movement is allowed, adjust neighbor exploration to 8 directions; otherwise, use 4 directions.
  • Avoid using DFS or Dijkstra's algorithm since all edges have unit weight; BFS is optimal.

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

Q2

Follow-up: how would you handle sources that move over time, where their positions change dynamically?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where I got a bit shaky.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints first, such as the number of moving sources, update frequency, and query types. Then propose a data structure that supports efficient updates and queries, discussing trade-offs between different approaches. Finally, outline how you would handle edge cases and scale the solution.

Pro tip: Mention that in real-world systems, you often need to balance update and query costs, and consider using a combination of data structures or lazy updates to optimize for the dominant operation.

1. Clarify Requirements

Ask about the number of sources, how often they move, the types of queries (e.g., nearest source, range queries), and performance requirements.

2. Choose Data Structure

Select a data structure that supports dynamic updates and efficient queries, such as a k-d tree with rebuilding, a quadtree, or a spatial index like R-tree.

3. Handle Updates

Describe how to update the data structure when a source moves, considering strategies like lazy deletion, periodic rebuilding, or incremental updates.

4. Optimize Queries

Explain how queries are performed efficiently, possibly using techniques like bounding boxes, pruning, or caching.

5. Discuss Trade-offs

Compare alternatives, highlighting time/space complexity, update vs. query performance, and suitability for different scenarios.

Key Points to Mention

  • Spatial indexing structures (k-d tree, quadtree, R-tree)
  • Dynamic updates: lazy deletion, periodic rebuilding, incremental updates
  • Trade-offs between update and query performance
  • Handling frequent moves: batching updates or using a time-based approach
  • Scalability: distributed systems or sharding for large numbers of sources
  • Edge cases: sources moving out of bounds, simultaneous updates, consistency

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