Magicsheet logo

Find the Minimum Amount of Time to Brew Potions

Medium
12.5%
Updated 8/1/2025

Find the Minimum Amount of Time to Brew Potions

What is this problem about?

In the Find the Minimum Amount of Time to Brew Potions coding problem, you are given a set of recipes, each requiring several ingredients and a specific time to process. Some ingredients are raw, while others are intermediate potions that must be brewed first. This is a dependency-based timing problem where you want to find the total time required to brew a target potion, assuming you can brew multiple things in parallel.

Why is this asked in interviews?

Companies like Goldman Sachs and Google use this to test your understanding of Graph interview patterns, specifically Directed Acyclic Graphs (DAGs) and Longest Path logic. It evaluations your ability to model dependencies and calculate completion times in a system with concurrency. It’s similar to how build systems (like Make or Bazel) calculate compilation times.

Algorithmic pattern used

This problem is solved using Topological Sort or DFS with Memoization.

  1. Represent potions and ingredients as nodes in a graph.
  2. If Potion A requires Potion B, create a directed edge BoAB o A.
  3. For each node, the "Time to Complete" is: node.processTime + max(Time to Complete all dependencies).
  4. If a node is a raw ingredient, its dependency time is 0.
  5. Use a Hash Map to cache the completion time for each potion to avoid redundant calculations.

Example explanation

Potion A: needs B and C, takes 5 mins. Potion B: needs D, takes 10 mins. Potion C: takes 2 mins. Raw D: takes 0 mins.

  1. Time(D) = 0.
  2. Time(B) = 10 + 0 = 10.
  3. Time(C) = 2.
  4. Time(A) = 5 + max(10,2)=15\max(10, 2) = 15. Total time: 15 minutes.

Common mistakes candidates make

  • Ignoring Parallelism: Adding all times together instead of taking the maximum of dependencies.
  • Cycles: Not checking if the recipe list contains a circular dependency, which would make brewing impossible.
  • Inefficient traversal: Re-calculating the time for the same sub-potion multiple times.

Interview preparation tip

When you see a problem with "dependencies" and "parallel execution," think about the longest path in a DAG. The total time is the duration of the "Critical Path." Practice using Topological Sort to process nodes in the order they become available.

Similar Questions