Avatar Component Loading Order with Dependencies and Cycle Detection

Quick Overview

Implement a loader that registers avatar components and their dependencies, returns the lexicographically smallest valid loading order, and reports circular dependencies. It tests topological sorting, deterministic tie-breaking, cycle detection and choosing a graph representation that keeps per-component queries cheap.

Avatar Component Loading Order with Dependencies and Cycle Detection

Company: Roblox

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a system that manages the loading sequence of avatar components such as hair, clothes, accessories and animations. Each component may depend on other components that must be loaded before it. The system determines a valid loading order that respects every dependency, and it reports a circular dependency when one exists. Components can be added at any time, and dependencies can be registered between components that already exist. The system also tracks which components have been marked as loaded, answers whether a component is ready (all of its direct dependencies are loaded), and answers queries about direct dependency relationships. Implement a class `AvatarComponentLoader` with these methods: - `add_component(name: str) -> None` registers a new component. Adding a name that is already registered has no effect. - `add_dependency(component: str, dependency: str) -> bool` records that `component` depends on `dependency`. Both must already exist. It returns `True` if the dependency was added and `False` otherwise (an unknown name, a component depending on itself, or a dependency that is already registered). - `get_load_order() -> List[str]` returns a valid loading order in which every dependency comes before the components that need it. When several components could come next, the lexicographically smallest name goes first. It returns an empty list if a circular dependency exists. - `has_cycle() -> bool` returns whether the components contain a circular dependency. - `mark_loaded(name: str) -> bool` marks a component as loaded. It returns `True` on success and `False` if the component does not exist or is already loaded. - `is_ready(name: str) -> bool` returns whether all direct dependencies of the component have been loaded. It returns `False` if the component does not exist. - `get_dependencies(name: str) -> List[str]` returns the direct dependencies of the component, sorted. - `get_dependents(name: str) -> List[str]` returns the components that directly depend on the given one, sorted. - `get_loaded_count() -> int` returns the number of components marked as loaded. - `get_component_count() -> int` returns the number of registered components. Both list getters return an empty list for an unknown name. For example: ```python loader = AvatarComponentLoader() for name in ["body", "hair", "hat", "shirt"]: loader.add_component(name) loader.add_dependency("hair", "body") # True loader.add_dependency("shirt", "body") # True loader.add_dependency("hat", "hair") # True loader.add_dependency("hat", "hair") # False, already registered loader.add_dependency("hat", "cape") # False, "cape" does not exist loader.get_load_order() # ["body", "hair", "hat", "shirt"] loader.is_ready("hair") # False, "body" is not loaded loader.mark_loaded("body") # True loader.mark_loaded("body") # False, already loaded loader.is_ready("hair") # True loader.is_ready("hat") # False, "hair" is not loaded loader.get_dependents("body") # ["hair", "shirt"] loader.get_dependencies("hat") # ["hair"] loader.get_loaded_count() # 1 loader.get_component_count() # 4 ``` `hat` comes before `shirt` because both are ready once `hair` is placed, and `hat` is the smaller name. A second example with a cycle: ```python loader = AvatarComponentLoader() for name in ["aura", "cape", "glow", "wings"]: loader.add_component(name) loader.add_dependency("wings", "cape") # True loader.add_dependency("glow", "wings") # True loader.add_dependency("cape", "glow") # True, accepted even though it closes a cycle loader.add_dependency("cape", "cape") # False, self-dependency loader.has_cycle() # True loader.get_load_order() # [] ``` `aura` has no dependencies, yet the whole order is empty because `cape`, `wings` and `glow` form a cycle. ```hint Pick an edge direction Draw each dependency as an arrow from the component that must load first to the component that needs it. A loading order is then a sequence in which every arrow points forward. Which graph algorithm builds that sequence, and how does it notice when it gets stuck? ``` ```hint Ties and lookups Several components can be ready at the same moment, and the smallest name must win every time, including names that only become ready later. For the per-component queries, think about what to store so that none of them has to scan every edge. ``` ### Constraints & Assumptions - Component names are unique strings of length 1 to 50. - At most 1000 components are added. - At most 5000 dependencies are registered. - The dependencies usually form a directed acyclic graph, but `add_dependency` does not reject an edge that closes a cycle, so the system must detect one. ### Clarifying Questions to Ask - Should `mark_loaded` refuse a component whose dependencies are not loaded yet, or does it only record state? - Is lexicographic order plain case-sensitive string comparison? - How often is `get_load_order` called compared with `add_dependency`? That decides whether caching the order is worth the bookkeeping. - When a cycle exists, does the caller only need to know that it exists, or also which components form it? ### What a Strong Answer Covers - A graph representation with a stated edge direction, and how each per-component query avoids scanning the whole graph. - How the load order is built so that ties always resolve to the smallest name and the output is unique. - How a cycle is detected, and how an empty system is told apart from a cyclic one. - Input validation in `add_dependency` and `mark_loaded`: unknown names, self-dependency, repeated edges and repeated loads. - The time and space cost of each method at the stated limits. ### Follow-up Questions - How would you add `remove_component` and `remove_dependency`, and what should happen to a loaded component whose dependency is removed? - If `has_cycle` is called after every `add_dependency`, how would you avoid rerunning a full sort each time? - How would you report the components that form a cycle? - How would you change the design so that independent components load in parallel?

Overview: Implement a loader that registers avatar components and their dependencies, returns the lexicographically smallest valid loading order, and reports circular dependencies. It tests topological sorting, deterministic tie-breaking, cycle detection and choosing a graph representation that keeps per-component queries cheap.

Roblox logo
Roblox
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a system that manages the loading sequence of avatar components such as hair, clothes, accessories and animations. Each component may depend on other components that must be loaded before it.

The system determines a valid loading order that respects every dependency, and it reports a circular dependency when one exists. Components can be added at any time, and dependencies can be registered between components that already exist. The system also tracks which components have been marked as loaded, answers whether a component is ready (all of its direct dependencies are loaded), and answers queries about direct dependency relationships.

Implement a class AvatarComponentLoader with these methods:

  • add_component(name: str) -> None registers a new component. Adding a name that is already registered has no effect.
  • add_dependency(component: str, dependency: str) -> bool records that component depends on dependency . Both must already exist. It returns True if the dependency was added and False otherwise (an unknown name, a component depending on itself, or a dependency that is already registered).
  • get_load_order() -> List[str] returns a valid loading order in which every dependency comes before the components that need it. When several components could come next, the lexicographically smallest name goes first. It returns an empty list if a circular dependency exists.
  • has_cycle() -> bool returns whether the components contain a circular dependency.
  • mark_loaded(name: str) -> bool marks a component as loaded. It returns True on success and False if the component does not exist or is already loaded.
  • is_ready(name: str) -> bool returns whether all direct dependencies of the component have been loaded. It returns False if the component does not exist.
  • get_dependencies(name: str) -> List[str] returns the direct dependencies of the component, sorted.
  • get_dependents(name: str) -> List[str] returns the components that directly depend on the given one, sorted.
  • get_loaded_count() -> int returns the number of components marked as loaded.
  • get_component_count() -> int returns the number of registered components.

Both list getters return an empty list for an unknown name.

For example:

loader = AvatarComponentLoader()
for name in ["body", "hair", "hat", "shirt"]:
    loader.add_component(name)

loader.add_dependency("hair", "body")    # True
loader.add_dependency("shirt", "body")   # True
loader.add_dependency("hat", "hair")     # True
loader.add_dependency("hat", "hair")     # False, already registered
loader.add_dependency("hat", "cape")     # False, "cape" does not exist
loader.get_load_order()                  # ["body", "hair", "hat", "shirt"]

loader.is_ready("hair")                  # False, "body" is not loaded
loader.mark_loaded("body")               # True
loader.mark_loaded("body")               # False, already loaded
loader.is_ready("hair")                  # True
loader.is_ready("hat")                   # False, "hair" is not loaded
loader.get_dependents("body")            # ["hair", "shirt"]
loader.get_dependencies("hat")           # ["hair"]
loader.get_loaded_count()                # 1
loader.get_component_count()             # 4

hat comes before shirt because both are ready once hair is placed, and hat is the smaller name.

A second example with a cycle:

loader = AvatarComponentLoader()
for name in ["aura", "cape", "glow", "wings"]:
    loader.add_component(name)

loader.add_dependency("wings", "cape")   # True
loader.add_dependency("glow", "wings")   # True
loader.add_dependency("cape", "glow")    # True, accepted even though it closes a cycle
loader.add_dependency("cape", "cape")    # False, self-dependency
loader.has_cycle()                       # True
loader.get_load_order()                  # []

aura has no dependencies, yet the whole order is empty because cape, wings and glow form a cycle.

Constraints & Assumptions

  • Component names are unique strings of length 1 to 50.
  • At most 1000 components are added.
  • At most 5000 dependencies are registered.
  • The dependencies usually form a directed acyclic graph, but add_dependency does not reject an edge that closes a cycle, so the system must detect one.

Clarifying Questions to Ask Guidance

  • Should mark_loaded refuse a component whose dependencies are not loaded yet, or does it only record state?
  • Is lexicographic order plain case-sensitive string comparison?
  • How often is get_load_order called compared with add_dependency ? That decides whether caching the order is worth the bookkeeping.
  • When a cycle exists, does the caller only need to know that it exists, or also which components form it?

What a Strong Answer Covers Guidance

  • A graph representation with a stated edge direction, and how each per-component query avoids scanning the whole graph.
  • How the load order is built so that ties always resolve to the smallest name and the output is unique.
  • How a cycle is detected, and how an empty system is told apart from a cyclic one.
  • Input validation in add_dependency and mark_loaded : unknown names, self-dependency, repeated edges and repeated loads.
  • The time and space cost of each method at the stated limits.

Follow-up Questions Guidance

  • How would you add remove_component and remove_dependency , and what should happen to a loaded component whose dependency is removed?
  • If has_cycle is called after every add_dependency , how would you avoid rerunning a full sort each time?
  • How would you report the components that form a cycle?
  • How would you change the design so that independent components load in parallel?