Problem · Design

Recipe Management System

Learn this problem
HardAirbnb logoAirbnbFULLTIMEOA

Problem statement

Process an ordered sequence of recipe and user operations. Recipe IDs are assigned monotonically as recipe1, recipe2, and so on, and deleted IDs are never reused.

Each operation is a string array with one of these forms:

  • ["ADD_RECIPE", name, ingredientsCsv, stepsCsv] adds a recipe when no existing recipe has the same name case-insensitively. It returns [recipeId], or an empty list on a name conflict.
  • ["GET_RECIPE", recipeId] returns [name, ingredientsCsv, stepsCsv], or an empty list when the ID is missing.
  • ["UPDATE_RECIPE", recipeId, name, ingredientsCsv, stepsCsv] replaces all recipe fields. It returns ["true"] on success and ["false"] for a missing ID or case-insensitive name conflict.
  • ["DELETE_RECIPE", recipeId] removes the recipe and returns a lowercase boolean string.
  • ["SEARCH_INGREDIENT", ingredient] returns IDs of recipes containing an exact case-insensitive ingredient. Order them by ingredient count ascending, then numeric recipe ID ascending.
  • ["LIST_RECIPES", sortBy] returns every recipe ID. For ingredient_count, sort by ingredient count ascending and then numeric ID. Otherwise, including an unsupported value, sort by case-insensitive name ascending and then numeric ID.
  • ["ADD_USER", username] returns ["true"] after adding a new case-sensitive username and ["false"] for a duplicate.
  • ["EDIT_RECIPE", username, recipeId, name, ingredientsCsv, stepsCsv] lets any existing user replace an existing recipe, subject to the same name rule, and returns a lowercase boolean string.

Return one string-list result for every operation in input order.

Function

manageRecipes(operations: String[][]) → String[][]

Examples

Example 1

operations = [["ADD_RECIPE","Pancakes","Flour,Egg,Milk","Mix,Cook"],["ADD_RECIPE","Salad","Lettuce,Tomato","Chop,Toss"],["GET_RECIPE","recipe1"],["SEARCH_INGREDIENT","egg"],["LIST_RECIPES","name"]]return = [["recipe1"],["recipe2"],["Pancakes","Flour,Egg,Milk","Mix,Cook"],["recipe1"],["recipe1","recipe2"]]

The ingredient lookup is case-insensitive, so egg finds recipe1. Name order places Pancakes before Salad.

Example 2

operations = [["ADD_RECIPE","Soup","Water,Carrot","Boil,Serve"],["ADD_RECIPE","sOuP","Stock","Heat"],["UPDATE_RECIPE","recipe1","Stew","Potato,Beef,Carrot","Brown,Simmer"],["GET_RECIPE","recipe1"],["DELETE_RECIPE","recipe9"],["DELETE_RECIPE","recipe1"],["GET_RECIPE","recipe1"]]return = [["recipe1"],[],["true"],["Stew","Potato,Beef,Carrot","Brown,Simmer"],["false"],["true"],[]]

The second add conflicts with Soup case-insensitively. Updating changes every stored field; deletion releases the name but never reuses the ID.

Example 3

operations = [["ADD_RECIPE","Beta","X,Y","One,Two"],["ADD_RECIPE","Alpha","Z","Only"],["ADD_USER","chef"],["ADD_USER","chef"],["EDIT_RECIPE","missing","recipe1","Nope","X","Only"],["EDIT_RECIPE","chef","recipe1","Gamma","X","Only"],["LIST_RECIPES","ingredient_count"],["LIST_RECIPES","unsupported"]]return = [["recipe1"],["recipe2"],["true"],["false"],["false"],["true"],["recipe1","recipe2"],["recipe2","recipe1"]]

Only an existing user can edit. After the edit, both recipes have one ingredient, so numeric ID breaks that tie. The unsupported sort key defaults to name order.

Constraints

  • 1 <= operations.length <= 10^4.
  • Every operation has a supported opcode and the exact arity shown in the statement.
  • Names, usernames, ingredients, and steps contain printable ASCII characters; names and usernames are non-empty.
  • Ingredient and step elements are non-empty, contain no commas, have length at most 100, and each CSV contains at most 100 elements.
  • Recipe-name equality and ordering and ingredient matching are case-insensitive; usernames are case-sensitive.

More Airbnb problems

drafts saved locally
public String[][] manageRecipes(String[][] operations) {
  // write your code here
}
operations[["ADD_RECIPE","Pancakes","Flour,Egg,Milk","Mix,Cook"],["ADD_RECIPE","Salad","Lettuce,Tomato","Chop,Toss"],["GET_RECIPE","recipe1"],["SEARCH_INGREDIENT","egg"],["LIST_RECIPES","name"]]
expected[["recipe1", "recipe2", "Pancakes", "Flour", "Egg", "Milk", "Mix", "Cook", "recipe1", "recipe1", "recipe2"]]
checking account