Airbnb questions

Recipe Management System

AirbnbOnline assessmentMedium

You are building a recipe manager as the class RecipeManagementSystem. Recipe ids are strings recipe1, recipe2, and so on, assigned in order of creation.

Part 1: Recipes with unique names

Implement:

  • add_recipe(name, ingredients, steps) creates a recipe and returns its id, or returns null, creating nothing, when a recipe with the same name already exists, ignoring case. Ids are recipe1, recipe2, and so on in order of creation. Only a successful add takes a number, and a number is never used again, even after its recipe is deleted.
  • get_recipe(recipe_id) returns [name, ingredients, steps] for the recipe, with the ingredients joined by "," and the steps joined by "," in their original order, or [] when the id does not exist.
  • update_recipe(recipe_id, name, ingredients, steps) replaces all three fields and returns true. It returns false, changing nothing, when the id does not exist or when name is the name of another recipe, ignoring case. Keeping a recipe's own name, in any casing, is allowed.
  • delete_recipe(recipe_id) deletes the recipe and returns true, or returns false when the id does not exist. Its name can be used again afterwards.

Recipe ids are case sensitive.

  1. Example 1
    init
    []
    operations
    [["add_recipe",["Pasta",["Tomato","Salt"],["Boil","Serve"]]],["get_recipe",["recipe1"]],["add_recipe",["pasta",["Egg"],["Fry"]]],["add_recipe",["Soup",["Water"],["Heat"]]],["get_recipe",["recipe9"]],["update_recipe",["recipe1","PASTA",["Tomato"],["Boil"]]],["get_recipe",["recipe1"]],["update_recipe",["recipe1","soup",["Tomato"],["Boil"]]],["update_recipe",["recipe5","Other",["Tomato"],["Boil"]]],["delete_recipe",["recipe1"]],["delete_recipe",["recipe1"]],["get_recipe",["recipe1"]],["add_recipe",["Pasta",["Rice"],["Cook"]]],["get_recipe",["recipe3"]]]
    Output
    ["recipe1",["Pasta","Tomato,Salt","Boil,Serve"],null,"recipe2",[],true,["PASTA","Tomato","Boil"],false,false,true,false,[],"recipe3",["Pasta","Rice","Cook"]]

    Why: The first recipe gets recipe1. Adding pasta fails, because names are unique ignoring case, and it does not use up an id, so Soup is recipe2. An unknown id reads as []. A recipe may be updated to a different casing of its own name, but not to another recipe's name. After recipe1 is deleted, the name Pasta is free again and the new recipe gets recipe3, not recipe1.

Constraints

  • 1 <= len(name) <= 32
  • Names, ingredients and steps use only ASCII letters, digits and spaces
  • 0 <= len(ingredients) <= 1000
  • 0 <= len(steps) <= 1000
  • At most 10^5 calls in total
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question