Can Inventory Satisfy a Nested Recipe?
Updated
Problem statement
There are n item types. For item i, ingredients[i][j] is an ingredient item and quantities[i][j] is how many units of it are required to make one unit of item i.
An item with an empty ingredient list is a raw material. inventory[i] gives the available units of each raw material; inventory entries for composite items are zero. Recipe dependencies have no cycles: an item can never depend on itself, directly or through other recipes.
Return whether the inventory contains enough raw materials to make amount units of target.
Function
canMakeRecipe(ingredients: int[][], quantities: int[][], inventory: int[], target: int, amount: int) → booleanExamples
Example 1
ingredients = [[],[],[0,1]]quantities = [[],[],[2,3]]inventory = [4,6,0]target = 2amount = 2return = trueTwo target items require four units of raw item 0 and six units of raw item 1, exactly matching inventory.
Example 2
ingredients = [[],[0],[1]]quantities = [[],[3],[2]]inventory = [5,0,0]target = 2amount = 1return = falseThe nested recipe expands to six units of raw item 0, but only five are available.
Example 3
ingredients = [[],[],[0],[1,2]]quantities = [[],[],[2],[1,3]]inventory = [6,1,0,0]target = 3amount = 1return = trueThe target needs one unit of raw item 1 and three copies of item 2, which expand to six units of raw item 0.
Constraints
1 <= n <= 1000.ingredients.length = quantities.length = inventory.length = n.ingredients[i].length = quantities[i].length.- Every ingredient index is valid, every quantity and
amountis positive, and recipe dependencies are acyclic. - Only raw items have nonzero inventory.
- Expanding the requested recipe visits at most
200000recipe nodes, counting repeated dependency paths. - Every fully expanded raw-material requirement fits in a signed 64-bit integer.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Course ScheduleOracle · ONSITE INTERVIEWOracle interview questions
- Package Install OrderVerily · ONSITE INTERVIEW
- Course SchedulePlaud · ONSITE INTERVIEW
- Audit Discounted Category AncestorsFaire · ONSITE INTERVIEW
- Detect a Cycle in a Directed GraphMakeMyTrip.com · ONSITE INTERVIEWMakeMyTrip.com interview questions
- Course ScheduleFlexTrade · ONSITE INTERVIEWFlexTrade interview questions
- Remove Redundant Task DependenciesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Course ScheduleBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Can All Courses Be FinishedC3.ai · ONSITE INTERVIEWC3.ai interview questions