KatInterview

Design an In-Memory Recipe Storage Engine

Data Structure DesignModel answer
HardModel answerData Structure Designdesign-dsarraysstrings

Last updated

Design and implement an in-memory storage manager supporting CRUD operations, custom identifier parsing, case-insensitive uniqueness, and controlled renames.

Design an in-memory storage manager, RecipeStore, to maintain recipe objects and perform standard CRUD operations under specific constraints.

Each recipe entity comprises:

  • id: A string identifier formatted as "recipe<n>" (e.g., "recipe1", "recipe2").
  • name: A string representing the name of the recipe.
  • ingredients: A list of strings.
  • steps: A list of strings.

Implement the following public operations:

  1. create(name: str, ingredients: List[str], steps: List[str]) -> str

    • Assigns a sequential string ID starting at "recipe1", "recipe2", etc. IDs must strictly increment and cannot be recycled after deletion.
    • Enforces unique recipe names in a case-insensitive manner (e.g., "Tiramisu" and "tIrAmIsU" conflict).
    • Preserves the original casing provided during creation.
    • Returns the generated recipe ID. If creation fails due to a duplicate name, raise an exception or handle it according to standard API safety patterns.
  2. get(recipe_id: str) -> Optional[Recipe]

    • Retrieves the recipe matching recipe_id.
    • Parses the ID string to extract the integer suffix. If the ID format is invalid or the recipe does not exist, returns None.
  3. update(recipe_id: str, updated_recipe: Recipe) -> bool

    • Replaces the attributes of an existing recipe.
    • Fails (returns False) if recipe_id is invalid, does not exist, or if recipe_id != updated_recipe.id.
    • Modifying the name is strictly restricted: only case-only changes are permitted (e.g., changing "Pasta" to "PASTA" is valid, but changing "Pasta" to "Pizza" is invalid).
    • On a successful update, future get calls must reflect the new casing.
  4. delete(recipe_id: str) -> bool

    • Removes the recipe associated with recipe_id.
    • Returns True if successfully deleted, or False if recipe_id is invalid or non-existent.
    • Once deleted, its name is freed, allowing new recipes to reuse that name.

Requirements & Edge Cases

  • ID Formatting: Valid IDs consist of the exact string "recipe" followed by a positive integer in canonical base-10 format without leading zeros (e.g., "recipe1" is valid; "recipe01", "recipe0", "recipe-1", "Recipe1", and "recipe" are invalid).
  • Immutability & Encapsulation: Returned objects should not allow consumers to mutate internal data store state directly.

Hints

Hint 1

Use two primary maps: one keyed by the parsed numeric ID to store the entity, and another keyed by the casefolded name mapping to the numeric ID for O(1) duplicate checks.

Hint 2

Validate the ID format cleanly at the boundary by stripping the prefix and ensuring the remainder contains strictly canonical positive decimal digits (no leading zeros).

Hint 3

When updating, verify that the casefolded version of the existing name equals the casefolded version of the new name before applying changes.