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!
Your bakery just started delivering cakes, and your delivery van needs to get across town fast.
You have a map of the roads between the neighborhoods in town, along with how many minutes it takes to drive each one. Some roads are one-way, so being able to drive from one neighborhood to another doesn't mean you can drive back the same way.
The road map comes as a dictionary that maps each neighborhood to a list of the roads leaving it. Each road is a tuple: the neighborhood it leads to, and how many minutes it takes to drive. Every neighborhood shows up as a key, even if no roads leave it. Travel times are whole numbers of minutes, and they're never negative.
Write a function get_fastest_route that takes the road map, a starting neighborhood, and a destination, and returns a list of the neighborhoods along the fastest route between them, starting with the starting neighborhood and ending with the destination.
If there's no way to drive from the start to the destination, return None. If either one isn't on the map, raise an exception.
For example:
If there's a tie for the fastest route, returning any of them is fine.
The route with the fewest roads isn't always the fastest one. In the example, Bakery → Riverside → Lakeview only uses two roads, but it takes 12 minutes.
Watch out for one-way roads. In the example, the fastest way back from Lakeview isn't just the way there, backward. It uses the one-way road from Market Square to Riverside, which we couldn't drive on the way there.
Roads can loop around in circles. Make sure your function can't get stuck going around one forever.
What should happen if the start and the destination are the same neighborhood?
We can do this in O((N + M) log N) time, where N is the number of neighborhoods and M is the number of roads.
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((N + M) log N) time and O(N + M) space, where N is the number of neighborhoods and M is the number of roads.
We finalize each neighborhood at most once, and we only check a neighborhood's roads when we finalize it. So we check each road at most once, and each check adds at most one entry to the heap. Along with the entry for the start, that's at most M + 1 entries going into the heap, and each one comes out at most once.
Each push and pop costs time proportional to the log of the heap's size. The heap never holds more than M + 1 entries, and N neighborhoods can't have more than N^2 roads between them (one each way between every pair). So each push and pop is O(log N²), which is the same as O(2 log N), or just O(log N).
That's O(M log N) for all the heap work, plus O(N) for finalizing neighborhoods and rebuilding the route at the end. All together, that's O(N + M log N), which fits inside O((N + M) log N), the way Dijkstra's running time is usually written.
For space, fastest_times, how_we_got_here, finalized, and the route we return each hold at most one entry per neighborhood, so they're O(N). The heap can hold up to M + 1 entries, which is O(M).
If our heap could lower a neighborhood's time in place, it would never hold more than N entries, and we'd only need O(N) space. Pushing duplicates and skipping the stale ones is simpler, and it works with whatever heap our language gives us.
If a map has a lot of roads (M close to N^2), the version that scans every neighborhood to find the closest one is actually faster: O(N²) beats O(N² log N). But real towns only have a few roads leaving each neighborhood, so the heap wins.
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?
Wanna review this one again later? Or do you feel like you got it all?
Mark as done Pin for review laterReset editor