← Questions
Shortest path in an unweighted graph: BFS versus DFS
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Meta's AI-enabled coding round, last asked Sep 2025. It tests graphs, bfs and dfs.
Explain how to find the shortest path between two nodes of an unweighted graph, without necessarily writing code. Walk through the algorithm step by step, give its time and space, and argue why it is correct. Then compare breadth-first search with a depth-first approach, and say what changes once edges have weights.
Interface
Unless you agree on a different interface with the interviewer, assume, if you do write code:
-
shortest_path(graph: dict[str, list[str]], src: str, dst: str) -> list[str] | None— the nodes of one shortest path fromsrctodst, orNoneifdstis unreachable. The graph may be directed or undirected.
Example
graph = {"A": ["B", "C"], "B": ["D"], "C": ["D", "E"], "D": ["F"], "E": ["F"], "F": []}
shortest_path(graph, "A", "F") -> ["A", "B", "D", "F"] # 3 edges; A C E F is also shortest
shortest_path(graph, "F", "A") -> None # edges are directed
shortest_path(graph, "A", "A") -> ["A"]
Constraints
- Up to
10^5nodes and10^6edges. The graph may contain cycles.
Clarifying questions
- Directed or undirected? Can there be cycles and self-loops?
- Do you want the length, or the path itself?
- Is the graph given as an adjacency list, or do we discover neighbors through a function?
- Are the weights all equal, or do some edges cost more?
What a strong answer covers
- BFS from
srcwith a queue, marking nodes visited when they are enqueued and keeping a parent pointer for each, then rebuilding the path fromdst. - Why BFS is correct: it settles nodes in order of distance, so the first time it reaches a node is along a shortest path. O(V + E) time and O(V) space.
- DFS does not find shortest paths by itself. It would need to explore every path, or run as iterative deepening, which gives depth-first memory with BFS's answer.
- When DFS is the better tool: cycle detection, topological order, connectivity, and memory on wide graphs.
- Weighted graphs: Dijkstra for non-negative weights, 0-1 BFS for weights 0 and 1, Bellman-Ford when weights can be negative.
- Bidirectional BFS for large graphs with one source and one target.
Follow-ups
- Return every shortest path, not just one.
- The graph is too large to load (a social network). How does bidirectional BFS help?
- Edge weights are 1 or 2. What is the cheapest algorithm?
Where it's asked
- Meta · AI-enabled coding · Last asked Sep 202560 min · Several progressive tasks on a starter codebase in CoderPad with an AI assistant panel; usually two rounds · The first task usually without AI, later tasks with it
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
