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 in!
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 dictionary that maps each step's name to a list 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 function get_step_order 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 None.
For example, here's a recipe for a frosted 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 function only needs to return one of them.
Some recipes are impossible! Does your function 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.
Log in or sign up with one click to get immediate access to free mock interview questions
We'll never post on your wall or message your friends.
Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?
Log in or sign up with one click to get immediate access to free mock interview questions
We'll never post on your wall or message your friends.
Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?
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 prereqs_left and dependent_steps touches each step once and each prerequisite once. That's O(V + E).
In the main loop, each step goes into ready_steps 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, dependent_steps holds a key for every step and an entry for every prerequisite, so it's O(V + E). prereqs_left, ready_steps, and step_order 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.
Log in or sign up with one click to get immediate access to free mock interview questions
We'll never post on your wall or message your friends.
Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?
Reset editor