Question presented to candidate: "You need shortest paths in a graph. When is a plain BFS enough, and when do you need Dijkstra? Show me both in Python and explain what goes wrong if you pick the wrong one."
What a strong answer should cover:
- BFS minimises the number of edges; Dijkstra minimises total weight. They agree exactly when all weights are equal (any equal value, not only 1).
- Why BFS is correct there: its FIFO queue already releases nodes in distance order, so the O(log V) heap is unnecessary.
- A concrete counterexample where BFS picks a fewer-edge but more expensive path.
- 0-1 BFS with a deque for weights 0 and 1, and Bellman-Ford when weights can be negative.
- Python specifics:
collections.dequewithpopleft()(O(1)) instead oflist.pop(0), andheapqfor Dijkstra.
Code / implementation expected: Yes โ a Python BFS with collections.deque and a Dijkstra with heapq, ideally plus 0-1 BFS with appendleft.