Went with BFS for unweighted, mentioned Dijkstra for weighted.
Start by clarifying the graph type (directed/undirected, weighted/unweighted) and the definition of 'shortest' (fewest edges or minimum weight). Then select the appropriate algorithm: BFS for unweighted graphs, Dijkstra for non-negative weights, or Bellman-Ford for negative weights, and explain the reasoning behind your choice.
Pro tip: At Amazon, interviewers value candidates who proactively discuss trade-offs and edge cases, so after presenting your solution, mention how you would handle large-scale graphs or negative cycles, and ask if the graph is static or dynamic.
Ask questions to determine if the graph is directed or undirected, weighted or unweighted, and whether edge weights can be negative. Also confirm if the graph is connected and if there are multiple shortest paths.
Based on the clarifications, select BFS for unweighted graphs, Dijkstra for non-negative weighted graphs, or Bellman-Ford for graphs with negative weights. Explain why the chosen algorithm is optimal for the given constraints.
Describe the step-by-step process: initialize distances, use a queue (BFS) or priority queue (Dijkstra), relax edges, and track predecessors to reconstruct the path. Mention time and space complexity.
Discuss edge cases such as disconnected graphs, negative cycles, or when the source and target are the same. Suggest optimizations like early termination in Dijkstra when the target is reached.
Explain how to reconstruct the shortest path using parent pointers and verify the solution with a small example. Mention that you would test with various graph types.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.