Asked an advanced algorithm question, how to write a function to copy all the nodes in a graph. Had to do this on the coderpad.io, always clunky at best.
Implement clone_graph(graph, start). The input graph is a dictionary mapping string node IDs to lists of neighboring string IDs. Return a new adjacency dictionary containing only nodes reachable from start, preserving every edge, duplicate, self-loop, and neighbor order. Return {} when start is absent. Do not mutate graph.
Input: graph: dict[str, list[str]], start: str. Output: dict[str, list[str]]. The graph can contain cycles and up to 500 reachable nodes.
def clone_graph(graph, start):