← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Google SWE interview with a graph/BFS problem that had two parts and a follow-up. The problem was dressed up as delivery route planning but it's basically shortest path with constraints. Felt manageable until part two where the optimization criteria got layered.

Questions Asked (3)

Q1

Given a set of delivery stops and routes (where adjacent stops in a route are connected), find the minimum number of steps from a source stop to a target stop without passing through any dangerous stops. Return -1 if source or target is dangerous, or if no safe path exists.

Algorithms & Data Structures
Author's notes

Pretty standard BFS once you see it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the stops and routes as an unweighted graph, then run BFS from the source to find the shortest path to the target while skipping dangerous stops. First check if source or target is dangerous and return -1 immediately; otherwise, BFS guarantees the minimum number of steps.

Pro tip: Explicitly state that BFS is optimal for unweighted graphs and mention that you would treat dangerous stops as blocked nodes, never enqueuing them. This shows you understand both the algorithm and the problem constraints.

1. Clarify and validate inputs

Confirm the graph representation (adjacency list or matrix), whether stops are 0-indexed or 1-indexed, and that dangerous stops are given as a set. Check if source or target is dangerous and return -1 immediately.

2. Build the graph

Construct an adjacency list from the given routes, ensuring it's undirected if routes are bidirectional. Exclude any edges that involve dangerous stops to simplify traversal.

3. Run BFS from source

Initialize a queue with the source stop and a visited set. While the queue is not empty, dequeue a stop, and if it's the target, return the current distance. Otherwise, enqueue all unvisited, non-dangerous neighbors with distance+1.

4. Handle unreachable target

If BFS completes without finding the target, return -1. Also ensure that the source and target are not dangerous before starting BFS.

5. Analyze complexity

State that time complexity is O(V + E) and space complexity is O(V) for the visited set and queue, where V is the number of stops and E is the number of routes.

Key Points to Mention

  • BFS is optimal for unweighted graphs to find shortest path
  • Dangerous stops are treated as blocked nodes and never enqueued
  • Early return -1 if source or target is dangerous
  • Use a visited set to avoid cycles and redundant work
  • Time and space complexity: O(V + E) time, O(V) space
  • Edge cases: source equals target, disconnected graph, no safe path

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

Q2

Extend the previous problem: now you can pass through dangerous stops. Among all paths from source to target, first minimize the number of dangerous stops visited, then minimize the number of steps. How do you find the optimal path under this two-level priority?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where I started sweating a bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a graph where each node tracks both the number of dangerous stops visited and the number of steps taken. Use a modified Dijkstra's algorithm with a priority queue ordered lexicographically by (dangerous count, steps) to find the optimal path. Alternatively, use 0-1 BFS if edge weights are binary, but Dijkstra is more general.

Pro tip: Emphasize that the two-level priority can be handled by encoding the cost as a pair and ensuring the priority queue compares them lexicographically. Also, mention that if the graph is large, we can optimize by only considering Pareto-optimal states (dangerous count, steps) for each node.

1. Define State and Cost

Define the state as (node, dangerous_count) and the cost as steps. The primary objective is to minimize dangerous_count, and the secondary is to minimize steps. This allows us to treat the problem as a shortest path on an expanded graph.

2. Choose Algorithm

Use Dijkstra's algorithm with a priority queue that orders by (dangerous_count, steps) lexicographically. Alternatively, if edge weights are 0 or 1, use 0-1 BFS with a deque, but Dijkstra is more straightforward for this two-level priority.

3. Initialize and Relax

Initialize distances for all (node, dangerous_count) pairs to infinity, except the source with dangerous_count = 0 (or 1 if source is dangerous) and steps = 0. During relaxation, update the state if the new dangerous_count is smaller, or if equal and steps are fewer.

4. Terminate and Reconstruct

Stop when the target node is popped from the priority queue with the minimal dangerous_count and steps. Reconstruct the path by keeping track of predecessors for each state.

5. Analyze Complexity

The time complexity is O((V + E) log V) for Dijkstra on the expanded graph, where V is the number of nodes times the maximum possible dangerous count. Space complexity is O(V) for distances and predecessors.

Key Points to Mention

  • Lexicographic ordering of the priority queue by (dangerous_count, steps).
  • Expanded graph where each node is (original_node, dangerous_count).
  • Dijkstra's algorithm is suitable because edge weights (steps) are non-negative.
  • Handling of dangerous stops: increment dangerous_count when visiting a dangerous node.
  • Path reconstruction using predecessor pointers for each state.
  • Potential optimization: only keep Pareto-optimal (dangerous_count, steps) pairs per node.

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

Q3

How would you change your visited structure from part one to correctly handle the two-criteria optimization in part two, specifically accounting for cases where the same stop can be reached with fewer dangerous stops or fewer total steps?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Follow-up came right after and I think I gave a decent answer but it was a bit rambly.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by restating the problem: we need to find a path that minimizes dangerous stops first, then total steps. Explain that a simple visited set is insufficient because a node might be reached with more dangerous stops but fewer total steps, and that state must be tracked as (node, dangerous_stops). Then describe how to adapt the visited structure to store the best (dangerous_stops, steps) pair for each node and only revisit when both criteria improve.

Pro tip: Emphasize that the visited structure must support dominance checks: a state is only worth exploring if it is not dominated by a previously seen state with both fewer or equal dangerous stops and fewer or equal steps. This shows you understand multi-criteria optimization beyond simple BFS.

1. Clarify the two criteria and their priority

Confirm that the primary objective is to minimize dangerous stops, and the secondary is to minimize total steps. This lexicographic order determines how we compare states.

2. Define the state representation

The state should be (node, dangerous_stops_so_far) because the same node can be reached with different numbers of dangerous stops, affecting future choices. Total steps is the value we optimize.

3. Design the visited structure

Use a map from node to a list of (dangerous_stops, steps) pairs that are Pareto-optimal. Alternatively, store the best steps for each (node, dangerous_stops) combination, but prune dominated entries.

4. Update and prune on new paths

When reaching a node with a new (dangerous_stops, steps), check if it is dominated by any existing entry. If not, add it and remove any entries it dominates. Only enqueue the state if it is not dominated.

5. Use a priority queue for exploration

Since we have two criteria, use a priority queue ordered by (dangerous_stops, steps) to ensure we explore states in lexicographic order, similar to Dijkstra but with a custom comparator.

Key Points to Mention

  • The visited structure must track multiple dimensions: node and dangerous stops count.
  • Dominance: a state (d1, s1) dominates (d2, s2) if d1 <= d2 and s1 <= s2, with at least one strict inequality.
  • Pareto frontier: for each node, maintain only non-dominated (dangerous_stops, steps) pairs.
  • Priority queue ordering: use lexicographic order (dangerous_stops, steps) to explore optimal paths first.
  • Complexity trade-off: more states may be explored, but pruning dominated states keeps it efficient.
  • Correctness: the first time we reach the target with minimal dangerous stops and minimal steps is the optimal answer.

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