Implement an actor/component model with component properties and lookup by component type
Company: Epic Games
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design and implement an `ActorComponentSystem` class for an actor/component model. Actors are entities identified by unique string names. Components are modular pieces of functionality, such as `MeshComponent` or `PhysicsComponent`, that can be attached to actors.
Each actor can have at most one component of each type. Each component stores arbitrary key-value string properties (for example, a `MeshComponent` might have a `filepath` property, and a `PhysicsComponent` might have a `mass` property).
The system supports creating and removing actors, attaching and detaching components, reading and writing component properties, and querying which actors have a given component type. It must provide these methods:
- `create_actor(actor_name: str) -> bool` creates a new actor. Returns `True` if it was created, `False` if an actor with that name already exists.
- `remove_actor(actor_name: str) -> bool` removes an actor and all its attached components. Returns `True` if removed, `False` if the actor was not found.
- `attach_component(actor_name: str, component_type: str) -> bool` attaches a component of the given type to the actor. Returns `True` if attached, `False` if the actor does not exist or already has a component of that type.
- `detach_component(actor_name: str, component_type: str) -> bool` detaches a component from the actor. Returns `True` if detached, `False` if the actor or the component was not found.
- `has_component(actor_name: str, component_type: str) -> bool` returns whether the actor has a component of the given type. Returns `False` if the actor does not exist.
- `set_property(actor_name: str, component_type: str, key: str, value: str) -> bool` sets a key-value property on a component attached to an actor. Returns `True` if set, `False` if the actor or the component was not found.
- `get_property(actor_name: str, component_type: str, key: str) -> str` returns the value of a property on a component, or an empty string if the actor, the component or the key is not found.
- `get_components(actor_name: str) -> List[str]` returns all component type names attached to the actor, sorted alphabetically. Returns an empty list if the actor does not exist.
- `get_actors_with_component(component_type: str) -> List[str]` returns all actor names that have the given component type attached, sorted alphabetically.
In the examples, each operation is called with the argument list at the same position, and the output lists the return value of each call (`null` for the constructor).
Example 1:
```text
Input:
operations = ["ActorComponentSystem","create_actor","attach_component","attach_component","attach_component","set_property","get_property","get_components","detach_component","has_component","get_property","set_property"]
arguments = [[],["Hero"],["Hero","PhysicsComponent"],["Hero","PhysicsComponent"],["Hero","MeshComponent"],["Hero","MeshComponent","filepath","hero.fbx"],["Hero","MeshComponent","filepath"],["Hero"],["Hero","MeshComponent"],["Hero","MeshComponent"],["Hero","MeshComponent","filepath"],["Hero","MeshComponent","filepath","other.fbx"]]
Output: [null,true,true,false,true,true,"hero.fbx",["MeshComponent","PhysicsComponent"],true,false,"",false]
```
The second attach of `PhysicsComponent` fails because `Hero` already has one. Once `MeshComponent` is detached, its `filepath` property goes with it, so reading it returns an empty string and writing it returns `false`.
Example 2:
```text
Input:
operations = ["ActorComponentSystem","create_actor","create_actor","create_actor","attach_component","attach_component","attach_component","get_actors_with_component","remove_actor","get_actors_with_component","get_property","create_actor"]
arguments = [[],["A"],["B"],["C"],["A","PhysicsComponent"],["B","PhysicsComponent"],["C","PhysicsComponent"],["PhysicsComponent"],["B"],["PhysicsComponent"],["B","PhysicsComponent","mass"],["A"]]
Output: [null,true,true,true,true,true,true,["A","B","C"],true,["A","C"],"",false]
```
All three actors get a `PhysicsComponent`. After `B` is removed it no longer appears in the query, reading a property from the removed actor returns an empty string, and creating `A` again returns `false` because `A` still exists.
```hint Start with the shape of the data
Write out the lookups each method needs: actor to its components, component to its properties. Which nested structure makes all of them a direct lookup, and which rule does that structure enforce for free?
```
```hint The query that goes the other way
`get_actors_with_component` asks from component type to actors, the opposite direction of everything else. What could you keep alongside the main structure so it does not scan every actor, and which methods would have to keep it up to date?
```
### Constraints & Assumptions
- Actor names and component type names are non-empty strings.
- Property keys and values are strings.
- Each actor has at most one component of each type.
- There are at most 10,000 actors in total.
- Each actor has at most 100 components, and each component has at most 100 properties.
### Clarifying Questions to Ask
- When a component is detached and later attached again, should its old properties come back, or does it start empty?
- Does "sorted alphabetically" mean plain string comparison (uppercase before lowercase), or case-insensitive order?
- Are component types an open set of strings, or should the system reject types it does not know?
- Will the system be called from one thread only, or must it handle concurrent callers?
### What a Strong Answer Covers
- A data model that gives direct lookups from actor to component to property and enforces one component per type.
- How `get_actors_with_component` avoids a full scan, and how that structure stays consistent through `detach_component` and `remove_actor`.
- The exact return value for every failure case: missing actor, missing component, duplicate attach and missing key.
- Deterministic sorted output for the two list queries, and what that sorting costs.
- Time and space complexity per method, including removing an actor that has many components.
### Follow-up Questions
- If `get_actors_with_component` were called every frame for thousands of actors, how would you remove the per-call sorting cost?
- How would you support typed property values, such as a float `mass` or a vector `position`, in place of strings?
- How would you answer "which actors have both `MeshComponent` and `PhysicsComponent`" efficiently?
- What would you change to make the system safe to call from several threads?
Overview: Implement an in-memory actor/component system where named actors hold at most one component of each type and each component stores string properties. It tests nested hash map design, keeping a reverse index consistent when components and actors are removed, and returning deterministic sorted query results.