Visa OA Interview Question: Count Prep-List Combinations

18 Views
No Comments

In cooking terms, a recipe is a prep-list combination of ingredients if you can prepare it by joining together the first several ingredients from the array, one after another, starting from the very beginning.

More formally, a string recipe is a prep-list combination of an array of strings ingredients if there exists some index i >= 0 such that:

recipe = ingredients[0] + ingredients[1] + ... + ingredients[i]

Example:

  • For ingredients = ["flour", "sugar", "eggs"], "flour" is a prep-list combination.
  • "floursugar" is also a prep-list combination.
  • "floursug" is not a prep-list combination.
  • "floureggs" is not a prep-list combination.

Task: Given two arrays of strings ingredients and recipes, determine for each recipe whether it is a prep-list combination of ingredients. Return an array of booleans of length recipes.length, where the i-th element is true if recipes[i] matches, and false otherwise.

Note: A solution with time complexity not worse than O(ingredients.length^2 * recipes.length) will fit within the execution time limit.

This problem asks whether each recipe matches a prefix concatenation of the ingredients array, meaning it must be formed by joining ingredients from index 0 onward without skipping or reordering. A simple solution is to build cumulative strings for the ingredient prefixes and compare each recipe against them. The key detail is that a valid recipe is not any concatenation of ingredient strings, but only a consecutive prefix starting from the first ingredient.

END
 0