I went recursive first because it felt cleaner to write under pressure, but then they asked me to redo it iteratively and I fumbled a bit managing the visited set with the explicit stack.
Clarify the graph representation and whether the graph is directed or undirected, then choose DFS (iterative or recursive) to explore from the start node while tracking visited nodes and parent pointers. Reconstruct the path from parent pointers if the target is found; otherwise report no path.
Pro tip: Mention that DFS does not guarantee the shortest path, and if the interviewer wants the shortest path, BFS is more appropriate. Also, discuss handling large graphs to avoid recursion depth issues by using an iterative approach.
Ask about graph type (directed/undirected), input format (adjacency list/edge list), and whether any path is acceptable or if shortest path is needed. Confirm that nodes are labeled and the graph may be disconnected.
Decide between recursive and iterative DFS based on graph size and recursion depth concerns. For iterative, use an explicit stack; for recursive, use a helper function with visited set and parent map.
Perform DFS from the start node, marking nodes as visited and recording the parent of each visited node. Stop early if the target node is reached.
If target is found, backtrack from target to start using the parent map to build the path. If DFS completes without finding the target, report that no path exists.
State time and space complexity (O(V+E) time, O(V) space) and discuss edge cases: start equals target, disconnected graph, cycles, and large graphs.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.