Problem, as an interviewer would state it: "Implement a depth-first search over a graph given as an adjacency list, returning the order in which nodes are visited. Discuss edge cases, runtime, recursive versus iterative, and alternatives."
Examples:
graph = { A: ["B", "C"], B: ["D"], C: ["D", "E"], D: ["F"], E: ["F"], F: [] }
dfs(graph, "A"); // ["A", "B", "D", "F", "C", "E"]
Clarifying questions expected:
- Directed or undirected, and may it contain cycles, self loops or repeated edges?
- Is the graph connected, or must every component be covered?
- Which order do you want: pre-order (visit on entry), post-order (visit on exit)?
Code / implementation expected: Yes — real, direct proof that recursive and iterative versions agree, and of the subtle iterative variant that does not.