The core of it is Kahn's algorithm but the twist is you can't just do a standard BFS and call it done.
Model the dependencies as a directed graph and perform a topological sort using Kahn's algorithm, but with a twist: at each iteration, collect all nodes with in-degree zero, sort them by their original declaration index, and emit them as a wave. Validate upfront that all dependencies are declared and detect cycles by checking if all nodes are emitted.
Pro tip: Clarify the deterministic wave-based rule upfront: it's not just any topological order, but a level-by-level emission where each wave is sorted by declaration index. This ensures reproducibility and matches the expected output.
Build a mapping from component names to their declaration indices and dependency lists. Check that every dependency is declared; if not, return an error immediately.
Construct an adjacency list for the dependency graph and compute the in-degree for each component (number of unmet dependencies).
Collect all components with in-degree zero, sort them by declaration index, and add them to the current wave.
For each wave, emit the sorted components, then for each component, decrement the in-degree of its dependents. Collect all dependents that reach zero in-degree, sort them by declaration index, and form the next wave. Repeat until no nodes remain.
If the total number of emitted components is less than the total number of components, a cycle exists; return an error. Otherwise, return the concatenated waves as the build order.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.