NumPy allowed · plain Python earns 10 bonus points
Implement topological sort for a data/ML pipeline DAG, determining a valid stage execution order from declared dependencies.
Implement topological_sort(dependencies). dependencies maps each stage name to a list of stages it depends on (must run first). Return a valid execution order (a list of ALL stage names, including those with no dependents) such that every stage appears after all of its dependencies. Use Kahn's algorithm; among stages with no remaining dependencies at a given point, process them in sorted (alphabetical) order for a deterministic result. Assume the graph is acyclic.
topological_sort(dependencies: dict[str, list[str]]) -> list[str]