You only have free questions left (including this one).

But it doesn't have to end here! Sign up for the 7-day coding interview crash course and you'll get a free Interview Cake problem every week.

You're opening a bakery, and you're writing software that tells your bakers what to do next.

Every recipe is a set of steps. Some steps have to wait for others—you can't frost a cake until it's cooled, and you can't cool it until it's baked. Other steps don't depend on anything, so you can do them whenever you like.

Each recipe comes as a hash map that maps each step's name to an array of the steps that have to be finished before it can start. Every step shows up as a key, even if it has no prerequisites.

Write a method getStepOrder that takes a recipe and returns a List of all of its steps in an order the bakers can actually follow, with every step coming after all of its prerequisites.

Lots of different bakers edit the recipes, so sometimes the rules contradict each other. If there's no way to do every step, return null.

For example, here's a recipe for a frosted cake:

Map<String, String[]> recipe = new HashMap<>(); recipe.put("preheat oven", new String[] {}); recipe.put("mix dry ingredients", new String[] {}); recipe.put("cream butter and sugar", new String[] {}); recipe.put("beat in eggs", new String[] {"cream butter and sugar"}); recipe.put("make batter", new String[] {"mix dry ingredients", "beat in eggs"}); recipe.put("pour into pan", new String[] {"make batter"}); recipe.put("bake", new String[] {"preheat oven", "pour into pan"}); recipe.put("cool cake", new String[] {"bake"}); recipe.put("make frosting", new String[] {}); recipe.put("frost cake", new String[] {"cool cake", "make frosting"}); List<String> stepOrder = getStepOrder(recipe); // one valid answer: // [preheat oven, mix dry ingredients, cream butter and sugar, // beat in eggs, make batter, pour into pan, bake, // cool cake, make frosting, frost cake]

Most recipes have more than one valid order. For this one, we could just as well make the frosting first, or cream the butter and sugar before preheating the oven. Returning any valid order is fine.

A recipe can have lots of valid orders. Your method only needs to return one of them.

Some recipes are impossible! Does your method notice, instead of getting stuck in an infinite loop or quietly returning just some of the steps?

Make sure every step ends up in the order exactly once, including steps with no prerequisites (like "make frosting") and steps that no other step is waiting on (like "frost cake").

We can do this in O(V + E) time, where V is the number of steps and E is the total number of prerequisites listed in the recipe.

Start your free trial!

Log in or sign up with one click to get immediate access to free mock interview questions

Where do I enter my password?

Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?

  1. It's easy and quick. No "reset password" flow. No password to forget.
  2. It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
  3. It makes it harder for one person to share a paid Interview Cake account with multiple people.

Start your free trial!

Log in or sign up with one click to get immediate access to free mock interview questions

Where do I enter my password?

Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?

  1. It's easy and quick. No "reset password" flow. No password to forget.
  2. It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
  3. It makes it harder for one person to share a paid Interview Cake account with multiple people.

O(V + E) time and O(V + E) space, where V is the number of steps and E is the total number of prerequisites in the recipe (the number of nodes and edges in our graph).

Setting up prereqsLeft and dependentSteps touches each step once and each prerequisite once. That's O(V + E).

In the main loop, each step goes into readySteps at most once: either at the very start or at the moment its count hits zero, and a count can only hit zero once. So we go through the loop at most V times. Inside the loop, we walk through the current step's dependents. Across the whole loop, that crosses each edge at most once, which is O(E). Altogether, that's O(V + E).

For space, dependentSteps holds a key for every step and an entry for every prerequisite, so it's O(V + E). prereqsLeft, readySteps, and stepOrder each hold at most one entry per step, so they're O(V).

We can't do better than O(V + E) time. Every step has to end up in our answer, and if we skipped even one prerequisite, we might miss a cycle or put a step too early.

  1. Say every step takes one hour, and you have as many bakers as you want. What's the fewest number of hours a recipe could take? (Hint: what if you handled the ready steps in rounds?) What if each step takes a different amount of time?
  2. When a recipe is impossible, the head baker wants to know which rules to fix. Can you return just the steps that form a cycle? Careful: not every leftover step is in the cycle. In the banana walnut bread recipe, "bake" is just stuck behind it.
  3. Suppose that whenever several steps are ready, we want to do the one that comes first alphabetically. What would you change, and how would it affect the time cost?
  4. Try solving this with depth-first search instead. How would you notice a cycle?

Start your free trial!

Log in or sign up with one click to get immediate access to free mock interview questions

Where do I enter my password?

Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?

  1. It's easy and quick. No "reset password" flow. No password to forget.
  2. It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
  3. It makes it harder for one person to share a paid Interview Cake account with multiple people.

Reset editor

. . .