Recipe Rating Service
Updated
Problem statement
Write a recipe service that keeps all of its data in memory. The input operations holds the commands. Carry them out one after another. The output holds one string list per command, in the same order as the commands.
The first recipe created is called recipe1, the second recipe2, the third recipe3, and the numbering keeps counting up from there. Users are plain ids. There is no separate sign-up step: any user id may create or rate recipes.
Part 1: Create and fetch recipes
["CREATE_RECIPE", authorId, name, ingredientsCsv]: create a recipe written byauthorId.ingredientsCsvlists its ingredients, joined by commas. Two recipes may have the same name. The result is[recipeId], the id of the new recipe.["GET_RECIPE", recipeId]: the result is[name, authorId, ingredientsCsv], with the ingredients exactly as they were given when the recipe was created. If the id matches no recipe, the result is[].["GET_AUTHOR_RECIPES", authorId]: list the ids of the recipes thatauthorIdwrote, oldest first. An author with no recipes gets[].
Part 2: Rate recipes
["RATE_RECIPE", userId, recipeId, score]:userIdgives the recipe a score from1to5. When the id matches no recipe, or whenuserIdwrote the recipe, the result is["false"]and nothing changes: authors may not rate their own recipes. Otherwise the rating is saved and the result is["true"]. A user has at most one rating per recipe, so if the user rated this recipe before, the new score replaces the old one.
Part 3: Find the best recipes
The average rating of a recipe is the sum of its current scores divided by the number of users who rated it. Compare averages exactly, as fractions: for example, 9 / 2 is higher than 4, and 4 / 3 ties with 8 / 6. A higher average ranks higher. If two recipes have the same average, the one created earlier, with the smaller number in its id, ranks higher. A recipe with no ratings has no average and is never part of the answer to these two operations.
["TOP_RATED", k]: give the ids of thekhighest-ranked rated recipes, best first. If fewer thankrecipes have ratings, give all of them, possibly none at all.["BEST_RECIPE_FOR", ingredientsCsv]: these are the ingredients you have. A recipe can be made if every one of its ingredients is in this list. The list may hold extra ingredients that the recipe never uses. The result is[recipeId]for the highest-ranked rated recipe that can be made. If no rated recipe can be made, the result is[].
Boolean results are always the lowercase strings "true" or "false".
Function
runRecipeService(operations: String[][]) → String[][]Examples
Example 1
operations = [["CREATE_RECIPE","ana","Pancakes","flour,egg,milk"],["CREATE_RECIPE","ben","Omelette","egg,cheese"],["CREATE_RECIPE","ana","Toast","bread,butter"],["GET_RECIPE","recipe2"],["GET_AUTHOR_RECIPES","ana"],["RATE_RECIPE","ben","recipe1","4"],["RATE_RECIPE","ana","recipe1","5"],["RATE_RECIPE","ana","recipe7","3"],["GET_RECIPE","recipe9"]]return = [["recipe1"],["recipe2"],["recipe3"],["Omelette","ben","egg,cheese"],["recipe1","recipe3"],["true"],["false"],["false"],[]]The three new recipes are numbered by creation order, recipe1 to recipe3. ana wrote recipe1 and recipe3. ben may rate recipe1, but ana may not rate her own recipe, and nobody can rate recipe7 because it does not exist. Fetching a missing recipe gives an empty list.
Example 2
operations = [["CREATE_RECIPE","ana","Soup","water,salt"],["CREATE_RECIPE","ben","Stew","water,beef"],["CREATE_RECIPE","cal","Rice","rice"],["RATE_RECIPE","ben","recipe1","4"],["RATE_RECIPE","ana","recipe2","5"],["RATE_RECIPE","cal","recipe2","3"],["RATE_RECIPE","cal","recipe1","2"],["RATE_RECIPE","cal","recipe1","4"],["TOP_RATED","2"],["TOP_RATED","5"]]return = [["recipe1"],["recipe2"],["recipe3"],["true"],["true"],["true"],["true"],["true"],["recipe1","recipe2"],["recipe1","recipe2"]]recipe1 has the scores 4 from ben and 4 from cal: cal first gave 2, and the second rating replaced it. Its average is 8 / 2 = 4. recipe2 has 5 and 3, also an average of 4. The tie goes to the recipe created first, so recipe1 is listed ahead of recipe2. recipe3 has no rating, so it is never listed, and asking for 5 recipes returns only the two rated ones.
Example 3
operations = [["CREATE_RECIPE","ana","Salad","lettuce,tomato"],["CREATE_RECIPE","ben","Sandwich","bread,tomato,ham"],["CREATE_RECIPE","cal","Tomato Toast","bread,tomato"],["CREATE_RECIPE","dee","Plain Bread","bread"],["RATE_RECIPE","ana","recipe2","5"],["RATE_RECIPE","ana","recipe3","4"],["RATE_RECIPE","ben","recipe1","3"],["BEST_RECIPE_FOR","bread,tomato,lettuce"],["BEST_RECIPE_FOR","bread,tomato,ham,cheese"],["BEST_RECIPE_FOR","bread"]]return = [["recipe1"],["recipe2"],["recipe3"],["recipe4"],["true"],["true"],["true"],["recipe3"],["recipe2"],[]]With bread, tomato and lettuce you can make recipe1 (average 3), recipe3 (average 4) and recipe4, but not recipe2, which also needs ham. recipe4 has no rating, so the best is recipe3. The second list, bread, tomato, ham and cheese, can make recipe2 (average 5), recipe3 and recipe4, so the best is recipe2; the unused cheese does not matter. With only bread, the one recipe you can make has no rating, so the answer is empty.
Constraints
1 <= operations.length <= 1000- Each operation takes one of the six forms described above, with the same number of values as that form.
- Every
authorIdanduserIdis 1 to 10 lowercase letters or digits. - Every
nameis 1 to 30 characters: letters, digits and single spaces, with no space at the start or end. - Every ingredient is 1 to 15 lowercase letters. An
ingredientsCsvis distinct ingredients joined by commas, with no spaces: 1 to 10 of them inCREATE_RECIPEand 1 to 20 inBEST_RECIPE_FOR. - Every
scoreis one digit from1to5. - Every
kis an integer with1 <= k <= 1000, written without leading zeros. - Every
recipeIdargument isrecipefollowed by an integernwith1 <= n < 10^9, written without leading zeros. The recipe it names might never have been created.
Similar questions at other companies
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Camel CardsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Banking System with Pending Transfer AcceptanceCircle · PHONE SCREENCircle interview questions
- Streaming Top-K Frequent ElementsNVIDIA · PHONE SCREENNVIDIA interview questions
- Versioned Snapshot SetOpenAI · PHONE SCREENOpenAI interview questions
- Interview Slot BookingTrueFoundry · PHONE SCREEN
- Config Store with Priority OverridesMicrosoft · PHONE SCREENMicrosoft interview questions
- Document Enrichment Stream AggregatorBloomberg LP · PHONE SCREENBloomberg LP interview questions
- In-Memory Database with TTL and Historical QueriesZip · PHONE SCREENZip interview questions
- Banking System with Top ActivityCapital One · ONSITE INTERVIEWCapital One interview questions