Notice Period

Produce the Build Order

medium

Same monorepo, harder ask: the CI system now needs the actual build order, not just a yes/no.

There are n packages numbered 0 to n - 1, and each rule [a, b] in deps means b must be built before a. Return a list of all n package numbers in a valid build order. If the rules are contradictory (cyclic), return an empty list [].

The test inputs are constructed so the valid order is unique — return exactly that order.

Example 1
in n = 3, deps = [[1, 0], [2, 1]]
out [0, 1, 2]
A chain: 0 unlocks 1, which unlocks 2.
Example 2
in n = 2, deps = [[0, 1], [1, 0]]
out []
Mutual dependency — no order exists.
Constraints
  • · 1 <= n <= 100
  • · 0 <= len(deps) <= 500
  • · deps[i] = [a, b] with 0 <= a, b < n
  • · when an order exists, it is unique
similar problem on LeetCode ↗
solution.pyloading python…
test results
Hit ▶ Run to test your code against the visible cases.