Practice/Anthropic/Recipe Manager (Online Assessment)
CodingMust
You are building an in-memory recipe management system for a cooking platform. This is a multi-level online assessment problem that progressively adds features:
Each level builds on the previous one. Subsequent levels unlock when you correctly solve the current level.
Time Limit: 90 minutes
Implement a recipe manager that supports creating, retrieving, updating, and deleting recipes.
add_recipe(recipe_id, name, ingredients) → bool
Creates a new recipe with the given ID, name, and list of ingredients. Returns True if successful, False if the recipe ID or name already exists (names are case-insensitive).
get_recipe(recipe_id) → dict | None
Retrieves the recipe as a dictionary containing id, name, and ingredients. Returns None if not found.
update_recipe(recipe_id, name, ingredients) → bool
Updates an existing recipe with new name and ingredients. Returns True if successful, False if the recipe doesn't exist or the new name conflicts with another recipe.
delete_recipe(recipe_id) → bool
Deletes the recipe. Returns True if successful, False if not found.
` manager = RecipeManager()
manager.add_recipe("1", "Pasta Carbonara", ["pasta", "eggs", "bacon"])
manager.add_recipe("2", "pasta carbonara", ["pasta"])
manager.get_recipe("1")
manager.update_recipe("1", "Classic Carbonara", ["pasta", "eggs", "bacon", "cheese"])
manager.delete_recipe("1")
`
Add search functionality with specific sorting rules.
search_recipes(query) → list[dict]
Searches for recipes where the name contains the query string (case-insensitive substring match). Returns a list of matching recipes sorted by:
Returns an empty list if no matches found.
get_all_recipes() → list[dict]
Returns all recipes sorted using the same rules as search_recipes.
` manager = RecipeManager() manager.add_recipe("3", "Caesar Salad", ["lettuce", "croutons"]) manager.add_recipe("1", "Pasta Carbonara", ["pasta", "eggs", "bacon", "cheese"]) manager.add_recipe("2", "Simple Pasta", ["pasta"])
manager.search_recipes("pasta")
manager.get_all_recipes()
`
Add user authentication and validation for recipe editing.
add_user(user_id, username) → bool
Creates a new user. Returns True if successful, False if user_id already exists.
edit_recipe(user_id, recipe_id, name, ingredients) → bool
Updates a recipe, but only if the user_id is valid. Returns True if successful, False if:
` manager = RecipeManager() manager.add_recipe("1", "Pasta", ["pasta", "sauce"]) manager.add_user("u1", "chef_john")
manager.edit_recipe("u1", "1", "Updated Pasta", ["pasta", "sauce", "cheese"])
manager.edit_recipe("u2", "1", "Another Update", ["pasta"])
`
Add version history tracking for recipes. Updates now create new versions instead of overwriting.
edit_recipe (now creates versions instead of overwriting)
Each edit appends a new version to the recipe's history. The most recent version is the "current" version. Version numbers start at 1.
get_recipe_history(recipe_id) → list[dict] | None
Returns the complete version history for the recipe, ordered from oldest to newest. Each entry includes version number, name, and ingredients. Returns None if recipe doesn't exist.
rollback_recipe(user_id, recipe_id, version) → bool
Creates a new version that is a copy of the specified historical version. Does NOT delete any versions. Returns True if successful, False if:
` manager = RecipeManager() manager.add_user("u1", "chef") manager.add_recipe("1", "Pasta", ["pasta"]) # Version 1
manager.edit_recipe("u1", "1", "Spaghetti", ["spaghetti", "sauce"]) # Version 2 manager.edit_recipe("u1", "1", "Linguine", ["linguine", "sauce"]) # Version 3
manager.get_recipe_history("1")
manager.rollback_recipe("u1", "1", 1) # Creates Version 4 = copy of Version 1
`
Case-Insensitive Name Conflicts
When rolling back, check if the old version's name conflicts with any OTHER recipe's current name:
` manager.add_recipe("1", "Pasta", ["pasta"]) manager.add_recipe("2", "Salad", ["lettuce"]) manager.edit_recipe("u1", "1", "Updated", ["pasta", "sauce"]) # v2 for recipe 1 manager.update_recipe("2", "Pasta", ["lettuce"]) # Recipe 2 now named "Pasta"
manager.rollback_recipe("u1", "1", 1)
`
Version Numbering