https://www.hack2hire.com/question-bank/companies/coinbase/coding-questions/69f2664c42791c82ab992693/practice?questionId=69f2665342791c82ab992694
这题很少出现,抽到了也是醉了。不过我拿了600/600,嘻嘻。我真厉害
This is a four-level Digital Recipe Manager problem. Level 1 is addRecipe / getRecipe / deleteRecipe: IDs auto-increment from "1", names are case-insensitive and a duplicate name returns an empty string, and getRecipe returns one flat array of name, then ingredients, then steps (empty if not found). Level 2 adds updateRecipe (can't rename to another recipe's name) plus search by ingredient (exact match, case-insensitive) and by name (substring, case-insensitive), both returning sorted IDs. Level 3 adds addUser and editRecipe, where the user must exist first, and starts version tracking: a version is only recorded if something actually changed, with the editor set to the userId, while updateRecipe always records a version with editor "system". Level 4 adds versionRecipe, which returns strings like "version:name:ingredientCount:stepCount:editor" in ascending version order, and rollbackRecipe, which restores an old version's content as a new version with editor "rollback" and fails if the old name now collides with another recipe.
A few gotchas visible in the code: addRecipe doesn't record a version, so the first edit is version 1. IDs are strings and sorted() is lexicographic, so "10" comes before "2", which may or may not be what the spec intends. And updateRecipe records a version even with no change while editRecipe doesn't, which is probably a deliberate rule in the problem.
class DigitalRecipeManagerImpl: def init(self): self.recipes = {} # id -> {"name", "ingredients", "steps"} self.id_by_lower_name = {} # lowercase name -> id self.history = {} # id -> list of version dicts self.next_id = 1 self.users = set()