KatInterview

Generate Unique Character Profiles with Attribute Rules

Data Structure DesignModel answer
MediumModel answerData Structure Designbacktrackingconstraint-satisfactionalgorithms

Last updated

Build a generator that creates N character profiles from trait pools, enforcing no-duplicates, uniqueness, and dependency rules between attributes.

You are building a character generator for a game. Given a collection of character attributes—each attribute has a name and a list of possible values—you need to generate N complete character profiles.

Input:

  • attributes: dict[str, list[str]] mapping attribute names to their possible values
    Example: {"class": ["warrior", "mage"], "weapon": ["sword", "staff", "bow"]}
  • count: int — number of profiles to generate
  • constraints: list[tuple] — optional dependency rules (see below)
  • all_distinct: bool — whether every generated profile must be unique

Constraints (applied in order of priority):

  1. Each profile assigns exactly one value to every attribute
  2. If all_distinct is true, no two profiles may share the same complete assignment
  3. Dependency rules:
    • ("must_pair", ("class", "mage"), ("weapon", "staff")) — if class is mage, weapon must be staff
    • ("cannot_pair", ("class", "warrior"), ("weapon", "staff")) — if class is warrior, weapon cannot be staff

Output: Return a list of count profile dictionaries. If generating count valid profiles is impossible, raise an exception with a message explaining why and attach any successfully generated profiles.

Your task: Implement the generator function. Explain your algorithm, data structures, time and space complexity, and how you handle edge cases. Describe what makes this problem hard and what trade-offs you considered.

Hints

Hint 1

Think of each profile as a constraint satisfaction problem; backtracking can explore valid assignments.

Hint 2

Index rules by the attributes they reference so you only check relevant constraints at each step.

Hint 3

Consider when to verify uniqueness: during generation or after? What's the complexity trade-off?