Compilation Order with Topological Sort
You are given a list of module names and a list of dependency pairs. Each dependency [a, b] means module a depends on module b, so b must be compiled before a.
Return one valid compilation order that includes every module exactly once. If multiple modules are currently available to compile, choose the lexicographically smallest available module first so the result is deterministic.
If the dependency graph contains a cycle, return a single-element array ["IMPOSSIBLE"].
Complete the function compilationOrder in the editor below.
compilationOrder has the following parameters:
String[] modules: all module namesString[][] dependencies: dependency pairs[module, prerequisite]
Returns
String[]: a valid compilation order, or ["IMPOSSIBLE"] if the dependencies contain a cycle.
1Example 1
Both B and C are initially available, so lexicographical order chooses B before C.
2Example 2
The two modules depend on each other, so no valid compilation order exists.
Constraints
Limits and guarantees your solution can rely on.
1 <= modules.length <= 2 * 10^50 <= dependencies.length <= 2 * 10^5- All module names are distinct.