Find All Possible Recipes from Given Supplies - Problem

Imagine you're a master chef with a cookbook of recipes and a pantry full of supplies. Your challenge is to determine which recipes you can actually cook!

You have n different recipes, where each recipe has a name and requires specific ingredients. Here's the twist: some recipes can serve as ingredients for other recipes. For example, you might need "dough" to make "pizza", but "dough" itself is another recipe in your cookbook.

Given:

  • recipes[] - Array of recipe names
  • ingredients[][] - 2D array where ingredients[i] contains all ingredients needed for recipes[i]
  • supplies[] - Array of ingredients you initially have (infinite supply)

Goal: Return a list of all recipes you can make, considering that recipes can depend on other recipes. The order doesn't matter.

Note: Two recipes may contain each other in their ingredients (circular dependencies), but you should only return recipes that can actually be completed.

Input & Output

basic_recipes.py — Python
$ Input: recipes = ["bread"], ingredients = [["yeast","flour"]], supplies = ["yeast","flour"]
Output: ["bread"]
💡 Note: We have yeast and flour, which are exactly the ingredients needed to make bread. Since we can make bread, it's included in the result.
recipe_dependencies.py — Python
$ Input: recipes = ["bread","sandwich"], ingredients = [["yeast","flour"],["bread","meat"]], supplies = ["yeast","flour","meat"]
Output: ["bread","sandwich"]
💡 Note: First we make bread using yeast and flour. Then we can make sandwich using the bread we just made plus meat from our supplies. Both recipes are possible.
impossible_recipe.py — Python
$ Input: recipes = ["bread","sandwich"], ingredients = [["yeast","flour"],["bread","meat"]], supplies = ["yeast"]
Output: []
💡 Note: We can't make bread because we're missing flour. Since bread can't be made, sandwich also can't be made (it needs bread as an ingredient). No recipes are possible.

Constraints

  • n == recipes.length == ingredients.length
  • 1 ≤ n ≤ 100
  • 1 ≤ ingredients[i].length ≤ 100
  • 1 ≤ supplies.length ≤ 100
  • 1 ≤ recipes[i].length, ingredients[i][j].length, supplies[k].length ≤ 10
  • All strings consist of only lowercase English letters
  • Each ingredients[i] does not contain duplicates

Visualization

Tap to expand
Find All Possible Recipes from Given Supplies INPUT recipes[]: "bread" ingredients[][]: [["yeast", "flour"]] bread needs: yeast + flour supplies[]: "yeast" "flour" Dependency Graph: bread yeast flour ALGORITHM STEPS 1 Build Graph Map recipe --> ingredients 2 Init Supplies Set Add all supplies to HashSet 3 Topological Sort Process with in-degree 4 Check Dependencies Verify all ingredients available Processing "bread": Check: "yeast" in supplies? OK Check: "flour" in supplies? OK All dependencies met! "bread" can be made FINAL RESULT Possible Recipes: "bread" Output Array: ["bread"] Verified: yeast: available flour: available bread: OK OK Key Insight: This problem is a dependency resolution task similar to topological sorting. We build a graph where recipes depend on ingredients (which can be supplies OR other recipes). Using BFS/DFS with memoization handles circular dependencies and ensures we only return recipes with all dependencies satisfiable. Time: O(N+E) TutorialsPoint - Find All Possible Recipes from Given Supplies | Optimal Solution (Topological Sort)
Asked in
Amazon 35 Google 28 Meta 22 Microsoft 15
39.2K Views
Medium Frequency
~25 min Avg. Time
1.7K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen