Problem · Design

Nested Recipe Inventory Manager

Learn this problem
MediumStackAdapt logoStackAdaptFULLTIMEPHONE SCREEN

Problem statement

Process operations on one recipe and raw-supply inventory manager. A recipe has a unique name, its own preparation time, and positive quantities of ingredients. An ingredient is either a previously added recipe or a raw ingredient.

Operations

  • ADD_RECIPE|name|time|ingredient:quantity,...: add a new recipe. Dependencies are listed before recipes that use them. Return OK.
  • ADD_SUPPLY|ingredient|count: add count units to a raw ingredient's inventory. Return OK.
  • PLACE_ORDER|dish: recursively expand the dish into total raw-ingredient requirements. If every requirement is available, deduct all of them and return true. Otherwise return false and leave every inventory count unchanged.
  • LIST_OVER|minutes: return the lexicographically sorted names of recipes whose own preparation time is strictly greater than minutes, joined by commas. Return an empty string when none match.

Return one result string for every operation, in input order.

Function

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

Examples

Example 1

operations = ["ADD_RECIPE|sauce|10|tomato:2","ADD_RECIPE|pasta|25|sauce:1,noodle:1","ADD_SUPPLY|tomato|4","ADD_SUPPLY|noodle|1","LIST_OVER|20","PLACE_ORDER|pasta","PLACE_ORDER|pasta","LIST_OVER|10"]return = ["OK","OK","OK","OK","pasta","true","false","pasta"]

One pasta needs two tomatoes through sauce and one noodle. The first order succeeds and consumes that inventory. The second fails because no noodle remains, so it consumes nothing. Only pasta has an own preparation time strictly above either queried threshold.

Example 2

operations = ["ADD_RECIPE|filling|30|bean:2,spice:1","ADD_RECIPE|wrap|15|filling:2,tortilla:1","ADD_SUPPLY|bean|4","ADD_SUPPLY|spice|2","ADD_SUPPLY|tortilla|1","PLACE_ORDER|wrap","LIST_OVER|15"]return = ["OK","OK","OK","OK","OK","true","filling"]

Expanding two units of filling requires four beans and two spices, so the order exactly consumes all supplied raw ingredients. The strict time comparison includes filling but not the 15-minute wrap.

Constraints

  • 1 <= operations.length <= 500
  • Names contain only lowercase English letters, are non-empty, and have length at most 30.
  • Recipe names are unique, dependencies form a directed acyclic graph, and every recipe dependency is added before the recipe that uses it.
  • Every ingredient quantity, supply addition, and preparation time is a positive integer at most 10^9.
  • Every expanded raw requirement and stored inventory total fits in a signed 64-bit integer.
  • Every PLACE_ORDER names an added recipe, and every operation follows its documented format.
drafts saved locally
public String[] runRecipeManager(String[] operations) {
    // Write your code here
}
operations["ADD_RECIPE|sauce|10|tomato:2","ADD_RECIPE|pasta|25|sauce:1,noodle:1","ADD_SUPPLY|tomato|4","ADD_SUPPLY|noodle|1","LIST_OVER|20","PLACE_ORDER|pasta","PLACE_ORDER|pasta","LIST_OVER|10"]
expected["OK", "OK", "OK", "OK", "pasta", "true", "false", "pasta"]
checking account