FastPrepFind All Possible Recipes from Given Supplies

Find All Possible Recipes from Given Supplies

Bloomberg LP logoBloomberg LP● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

You are given recipe names, the ingredients required by each recipe, and initially available supplies. A completed recipe becomes an available supply and may unlock other recipes.

Return every recipe that can eventually be produced, in the same order that recipes appear in the input.

Function

findAllRecipes(recipes: String[], ingredients: String[][], supplies: String[]) → String[]

Examples

Example 1

recipes = ["bread"]ingredients = [["yeast","flour"]]supplies = ["yeast","flour","corn"]return = ["bread"]

Both ingredients are initially available.

Example 2

recipes = ["bread","sandwich"]ingredients = [["yeast","flour"],["bread","meat"]]supplies = ["yeast","flour","meat"]return = ["bread","sandwich"]

Bread is produced first and then serves as an ingredient for sandwich.

Constraints

  • 1 <= recipes.length == ingredients.length <= 10^5.
  • Each recipe has at least one distinct ingredient.
  • Recipe names are unique, supply names are unique, and total ingredient entries are at most 2 * 10^5.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String[] findAllRecipes(String[] recipes, String[][] ingredients, String[] supplies) {
    // Write your code here.
}
recipes["bread"]
ingredients[["yeast","flour"]]
supplies["yeast","flour","corn"]
expected["bread"]
Checking account…