← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Graph question at Google, felt pretty standard until I started second-guessing my BFS approach mid-interview. The edge cases are where it gets messy and I didn't handle all of them as cleanly as I should have.

Questions Asked (1)

Q1

Given a directed graph and a target node, find the shortest cycle that passes through the target node, or determine that no such cycle exists.

Algorithms & Data Structures
Author's notes

My first instinct was DFS and I had to walk it back, which cost me time.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints (e.g., graph size, edge weights, whether the cycle can repeat nodes) and then propose an efficient algorithm. A common approach is to run BFS from the target node to find the shortest path back to itself, or to use Dijkstra if weights are non-negative. Discuss time and space complexity and edge cases.

Pro tip: Mention that the shortest cycle through a node can be found by removing the target node, finding the shortest path from each neighbor to the target, and taking the minimum sum plus the edge back. This shows deep understanding and avoids pitfalls with self-loops.

1. Clarify requirements and constraints

Ask about graph size, whether edges have weights, if negative weights exist, and if the cycle can repeat vertices. This determines the appropriate algorithm and complexity.

2. Choose the right algorithm

For unweighted graphs, BFS from the target is optimal. For weighted graphs with non-negative weights, Dijkstra from the target works. If negative weights are possible, discuss Bellman-Ford or Floyd-Warshall.

3. Outline the algorithm

Explain how to find the shortest cycle: e.g., run BFS/Dijkstra from the target, and when you encounter an edge back to the target, that forms a cycle. Keep track of the minimum distance.

4. Analyze complexity and edge cases

State time and space complexity (e.g., O(V+E) for BFS, O(E log V) for Dijkstra). Discuss edge cases: no cycle, self-loop, multiple edges, disconnected graph.

5. Test with examples

Walk through a small example to verify the algorithm and demonstrate correctness. Mention potential optimizations or alternative approaches.

Key Points to Mention

  • BFS for unweighted graphs, Dijkstra for weighted graphs with non-negative weights
  • Handling negative weights: Bellman-Ford or Floyd-Warshall
  • Time and space complexity analysis
  • Edge cases: no cycle, self-loop, multiple edges, disconnected components
  • Correctness proof: why the algorithm finds the shortest cycle
  • Alternative approach: remove target, find shortest path from each neighbor to target, add edge back

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