← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026Remote

Summary

Google SWE interview with a graph problem that started straightforward and then got a follow-up twist that actually wasn't as bad as I expected.

Questions Asked (2)

Q1

Given a weighted graph with non-negative edge weights, a start node S and a target node T, determine if T is reachable from S and if so return the shortest distance. Return -1 if not reachable.

Algorithms & Data Structures
Author's notes

Classic Dijkstra setup.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use Dijkstra's algorithm with a priority queue to compute shortest distances from S, then check if T is reachable. Since edge weights are non-negative, Dijkstra's is optimal; alternatively, BFS works only if all weights are equal. Return the distance to T or -1 if unreachable.

Pro tip: Mention that you can early-exit when T is popped from the priority queue, and discuss handling large graphs with lazy deletion to avoid stale entries. This shows awareness of practical optimizations and memory management.

1. Clarify assumptions and edge cases

Confirm that edge weights are non-negative, the graph may be directed or undirected, and nodes are labeled 0 to N-1. Discuss edge cases: S equals T, disconnected graph, and large input size.

2. Choose the right algorithm

Select Dijkstra's algorithm for non-negative weights. If all weights are equal, BFS is simpler and more efficient. Explain why Dijkstra's is correct and its time complexity O((V+E) log V).

3. Outline the algorithm steps

Initialize distances to infinity, set dist[S]=0, and push S into a min-heap. Repeatedly pop the node with the smallest distance, relax its neighbors, and update distances if a shorter path is found.

4. Handle termination and unreachable case

Stop when T is popped (early exit) or when the heap is empty. If T was never reached, return -1. Otherwise, return dist[T].

5. Analyze complexity and optimizations

State time complexity O((V+E) log V) and space O(V+E). Mention optimizations like using a Fibonacci heap (theoretical) or lazy deletion with a visited set to skip stale entries.

Key Points to Mention

  • Dijkstra's algorithm is optimal for non-negative weights; BFS only works for unweighted graphs.
  • Use a priority queue (min-heap) to efficiently extract the node with the smallest tentative distance.
  • Early termination when T is extracted from the heap can save time.
  • Handle unreachable nodes by returning -1 after the heap is exhausted.
  • Time complexity: O((V+E) log V) with a binary heap; space complexity: O(V+E).
  • Edge cases: S == T (return 0), disconnected graph, and large graphs requiring memory-efficient data structures.

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

Q2

Follow-up: now there's a mandatory waypoint node K that the path must pass through. Find the shortest path from S to T that goes through K, or return -1 if no such path exists.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where it got interesting.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize that the shortest path from S to T through K is the concatenation of the shortest path from S to K and the shortest path from K to T. Run Dijkstra's algorithm twice: once from S to compute distances to all nodes, and once from K (or from T on the reversed graph) to compute distances from K to all nodes. Then sum the distances at K and check for reachability.

Pro tip: Mention that if the graph is undirected, you can run Dijkstra from K once and reuse distances for both segments, but if directed, you need two runs (or one from S and one from K). Also, clarify that the path must be simple? Actually, shortest path with positive weights is automatically simple, but if negative weights exist, use Bellman-Ford and watch for negative cycles.

1. Clarify graph properties

Ask about directed vs undirected, weighted vs unweighted, and whether edge weights are non-negative. This determines the algorithm choice (Dijkstra vs Bellman-Ford vs BFS).

2. Decompose the problem

Explain that the shortest S→T path through K is the sum of the shortest S→K path and the shortest K→T path. This holds because any path through K can be split at K, and minimizing each segment independently minimizes the total.

3. Choose and run shortest path algorithm

For non-negative weights, run Dijkstra from S to get dist(S, K), and run Dijkstra from K to get dist(K, T). For negative weights, use Bellman-Ford from S and from K (or from T on reversed graph).

4. Combine and check reachability

If either dist(S, K) or dist(K, T) is infinity, return -1. Otherwise, return the sum. Mention that if S == K or K == T, one segment is zero.

5. Analyze complexity and edge cases

State time complexity: O((V+E) log V) for two Dijkstra runs. Discuss edge cases: K unreachable from S or T, graph with negative cycles, and multiple edges.

Key Points to Mention

  • Dijkstra's algorithm for non-negative weights, Bellman-Ford for negative weights
  • Two separate shortest path computations: S→K and K→T
  • Reachability check: if either segment is unreachable, return -1
  • Time complexity: O((V+E) log V) for Dijkstra, O(VE) for Bellman-Ford
  • Handling directed vs undirected graphs (for undirected, one Dijkstra from K suffices)
  • Edge cases: S == K, K == T, negative cycles, disconnected graph

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