As reported: MongoDB Senior Software Engineer (2026): a topological-sort / build-system problem where package dependency information arrives iteratively; in each iteration, unlock the packages that have become buildable and print them.
A build system learns about packages one at a time. Implement a BuildSystem class:
BuildSystem()starts with no packages.addPackage(name, deps)declares packagename, which depends on the packages indeps. It returns the list of packages that became buildable because of this call — these are built immediately.
A package is buildable once it has been declared and every one of its dependencies has been built. Building a package can make other, already declared packages buildable in turn; they are built in the same call and included in its result. A dependency that has not been declared yet blocks its dependents until it is declared and built. Packages caught in a dependency cycle never become buildable.
Example
addPackage("app", ["lib", "utils"]) -> [] utils and lib unknown yet
addPackage("utils", []) -> ["utils"]
addPackage("lib", ["utils"]) -> ["app", "lib"] lib builds, which unlocks app
addPackage("x", ["y"]) -> []
addPackage("y", ["x"]) -> [] x and y wait on each other forever
Constraints: up to 10^4 calls; each package is declared at most once; deps holds distinct names and at most 20 of them; names are 1–10 lowercase letters or digits; a package may list itself (it then never builds).
Code runner format: two JSON values, one per line: the list of names passed to addPackage, in call order, then the list of their deps lists. The harness calls addPackage for each and prints the list of results; each result is sorted before printing, so return the packages in any order.
Clarifications
- Only packages built during this call are returned — never ones built earlier.
- The returned list includes
nameitself when it builds right away.