Daily iOS
CS fundamentals and algorithms

Topological sort of dependencies

In an app split into 30 modules, the DI container initializes services in an order someone maintains by hand, and it crashed with "A needs B and B needs A." What algorithm gives you a safe initialization order from the dependency graph?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

Treat dependencies as a directed graph (a DAG) and get the initialization order from a topological sort, either Kahn's in-degree queue or reversed DFS post-order. The same idea is SPM's module graph, where changing a hub module recompiles everything downstream.

Share a question