Medium data structure and algorithm practice puzzle.
Given a network as an adjacency list and two routers, return the fewest hops between them, or -1 when no path exists. Every link costs the same here, which is exactly the condition that makes breadth-first search correct: it expands in rings of equal distance, so the first time it reaches a router it has arrived by a shortest path. Swap the queue for a stack and you still find a path, just not the short one.
Solve it three ways: snap the jigsaw pieces together, fill in the blanks, or type it from scratch against the tests. This one is free.
Browse all 536 puzzles