← Google Interview Insights

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

IntermediatePrefer not to say
Jun 2026

Summary

Google SWE coding round with a graph/shortest-path problem that had two parts. The second part is where things get interesting and a bit tricky.

Questions Asked (1)

Q1

You're given a graph of delivery stops, a start, an end, and a list of dangerous stops. First, find the shortest path that avoids all dangerous stops entirely, returning -1 if none exists. Second, find the shortest path that passes through the fewest dangerous stops possible.

Algorithms & Data Structures
Author's notes

Part one felt manageable, just run Dijkstra and skip any dangerous node during traversal.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as two shortest-path computations: first, remove dangerous stops and run BFS/Dijkstra to find the shortest safe path; second, use a modified Dijkstra where the primary cost is the number of dangerous stops and the secondary cost is path length. Clearly explain the graph representation, algorithm choice, and complexity trade-offs.

Pro tip: Mention that the second problem can be solved by assigning a weight of 1 to dangerous stops and 0 to safe stops, then using 0-1 BFS or Dijkstra with a lexicographic cost (dangerous count, distance) to efficiently find the optimal path.

1. Clarify the problem and constraints

Ask about graph size, whether edges are weighted, if the graph is directed, and if multiple paths with the same dangerous count should be compared by length. This ensures you handle edge cases correctly.

2. Solve the safe path problem

Remove all dangerous stops from the graph and run BFS (for unweighted) or Dijkstra (for weighted) from start to end. If no path exists, return -1.

3. Solve the fewest dangerous stops problem

Use a modified Dijkstra where each node's cost is a pair (number of dangerous stops, total distance). Alternatively, assign weight 1 to dangerous stops and 0 to safe stops, then use 0-1 BFS to find the path with minimum dangerous stops, breaking ties by distance.

4. Analyze complexity and edge cases

Discuss time and space complexity for both algorithms, and consider edge cases like start or end being dangerous, disconnected graphs, and multiple optimal paths.

5. Summarize and test

Walk through a small example to verify correctness, and mention potential optimizations or alternative approaches (e.g., A* for large graphs).

Key Points to Mention

  • Graph representation (adjacency list vs. matrix) and its impact on performance
  • BFS for unweighted graphs and Dijkstra for weighted graphs
  • 0-1 BFS or Dijkstra with lexicographic cost for the second problem
  • Handling of dangerous stops: removal for first part, cost assignment for second part
  • Time and space complexity: O(V+E) for BFS, O((V+E) log V) for Dijkstra
  • Edge cases: start/end dangerous, no path, multiple paths with same dangerous count

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