As reported: Roblox phone screen (2025): optimize avatar load times by loading components in dependency order.
An avatar is made of components — body parts, accessories, animations — and some can only load after others: a hat needs the head and the hair first. You are given a list of components, each written as a list of strings: the first string is the component's name, the rest are the components it depends on.
Return an order in which to load all components so that every component comes after all of its dependencies. When several orders are valid, return the one that preserves the relative order of the input: at every step, load the available component that appears earliest in the input. If the dependencies contain a cycle or name a component that is not in the list, return ["Error!"].
Examples
[["Head"], ["Torso"], ["Legs"]] -> ["Head", "Torso", "Legs"]
[["Hair", "Head"], ["Hat", "Hair"], ["Head"]] -> ["Head", "Hair", "Hat"]
[["Hair", "Head"], ["Head", "Hair"]] -> ["Error!"] (cycle)
[["Hair", "Head"], ["Head"], ["Torso"], ["Arm", "Torso"]] -> ["Head", "Hair", "Torso", "Arm"]
In the last example both Head and Torso can load first; Head comes earlier in the input, and once it is loaded Hair (input position 0) is available and goes before Torso.
This is a variant of LeetCode 210 — Course Schedule II with a fixed tie-break.
Constraints: 1 ≤ components ≤ 10^4; at most 10^5 dependencies in total; names are 1–20 letters or digits, and every component appears exactly once as a first element.
Clarifications
- A component that depends on itself is a cycle.
- A dependency listed twice for the same component counts once.
Code runner format: the first line is the number of components n; each of the next n lines holds one component's strings separated by spaces (name first). Implement loadOrder(components); the harness prints the returned names separated by spaces.