Sparse Set Component Storage for an Entity Component System
Last updated: November 29, 2026
Quick Overview
Implement a component store for an ECS that supports add, remove, has and get in O(1) and iterates components as a tightly packed array, then use it to iterate entities that have two component types.
Unity
November 29, 20260
0
0 solved
Implement a component store for an ECS that supports add, remove, has and get in O(1) and iterates components as a tightly packed array, then use it to iterate entities that have two component types.
Unity's DOTS stack is built around data-oriented layouts so systems can stream through components with good cache behaviour. This question checks whether a candidate can design a packed structure with swap-remove and reason about why layout, not big-O alone, drives game performance.
What the Interviewer Expects
- Keep dense entity and value arrays plus a sparse index map
- Implement swap-with-last removal and fix the moved entity's index
- Make add idempotent (overwrite existing)
- Join two component sets by iterating the smaller one
- Explain cache locality and iteration order trade-offs
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Swap-remove changes iteration order; when does that matter and how would you keep order?
- How do archetype chunks (as in Unity Entities) differ from sparse sets?
- How would you make structural changes safe while a system is iterating?
- How would you handle entity id reuse with generation counters?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Operations: add(entity, value), remove(entity), has(entity), get(entity), iteration over (entity, value); plus a two-component join such as Position and Velocity. All point operations O(1); ...
High-Level Design
A sparse set holds three parts: sparse maps entity id to an index, entities is the dense list of ids and data is the parallel dense list of values. Iteration walks the dense arrays with no holes...