← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
May 2026Remote

Summary

Google SWE coding round with a graph shortest path problem. Pretty standard Dijkstra territory but the directed/weighted combo tripped me up a bit on edge cases.

Questions Asked (1)

Q1

Given a weighted directed graph, find the shortest path from a given source node to all other nodes. Return the total time to reach all nodes via shortest paths, or -1 if any node is unreachable.

Algorithms & Data Structures
Author's notes

Went with Dijkstra pretty quickly, which was the right call, but I fumbled the unreachable node check at first.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize this as the classic single-source shortest path problem on a weighted directed graph with non-negative weights, so Dijkstra's algorithm is optimal. Use a min-heap to efficiently extract the next closest node, relax edges, and track distances. After computing distances, check for unreachable nodes (distance = infinity) and return -1 if any exist; otherwise return the maximum distance as the total time.

Pro tip: Clarify edge weight constraints upfront—if negative weights are possible, Dijkstra fails and Bellman-Ford is needed. Mentioning this shows you consider edge cases and algorithm applicability, which interviewers value.

1. Clarify problem constraints

Ask about edge weights (non-negative?), graph size, and whether the graph is connected. Confirm that the goal is to find shortest distances from one source to all nodes and return the maximum of those distances, or -1 if any node is unreachable.

2. Choose the right algorithm

For non-negative weights, Dijkstra's algorithm with a min-heap is optimal (O((V+E) log V)). If negative weights are allowed, use Bellman-Ford (O(VE)) and handle negative cycles.

3. Implement Dijkstra's algorithm

Initialize distances to infinity, set source distance to 0, and push source into a min-heap. While the heap is not empty, pop the node with the smallest distance, skip if already processed, and relax all outgoing edges, updating distances and pushing improved nodes.

4. Check reachability and compute result

After the algorithm, scan all distances. If any node (except possibly the source) has distance infinity, return -1. Otherwise, return the maximum distance, which represents the time for all nodes to receive the signal.

5. Analyze complexity and test

State time complexity O((V+E) log V) and space O(V+E). Walk through a small example, including a disconnected case, to verify correctness.

Key Points to Mention

  • Dijkstra's algorithm is optimal for non-negative weights; use a min-heap for efficiency.
  • Relaxation step: if dist[u] + weight < dist[v], update dist[v] and push to heap.
  • Track visited nodes to avoid reprocessing and ensure O((V+E) log V) time.
  • After computation, check for unreachable nodes (distance = infinity) and return -1 if any.
  • The total time is the maximum shortest distance from the source to any node.
  • Edge cases: source isolated, graph disconnected, single node, zero-weight edges.

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