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.

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 Map from each neighborhood to an array of the Roads leaving it. Each Road has 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 method getFastestRoute 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 null. If either one isn't on the map, throw an exception.

For example:

// each road leads to a neighborhood, and takes some number of minutes to drive public static class Road { public final String to; public final int minutes; public Road(String to, int minutes) { this.to = to; this.minutes = minutes; } } Map<String, Road[]> roadMap = new HashMap<>(); roadMap.put("Bakery", new Road[] {new Road("Old Town", 4), new Road("Riverside", 2)}); roadMap.put("Old Town", new Road[] {new Road("Bakery", 4), new Road("Riverside", 1), new Road("Market Square", 5)}); roadMap.put("Riverside", new Road[] {new Road("Bakery", 2), new Road("Old Town", 1), new Road("Lakeview", 10)}); roadMap.put("Market Square", new Road[] {new Road("Old Town", 5), new Road("Riverside", 1), new Road("Lakeview", 2)}); roadMap.put("Lakeview", new Road[] {new Road("Riverside", 10), new Road("Market Square", 2)}); roadMap.put("Hilltop", new Road[] {new Road("Lakeview", 3)}); getFastestRoute(roadMap, "Bakery", "Lakeview"); // [Bakery, Riverside, Old Town, Market Square, Lakeview] // (2 + 1 + 5 + 2 = 10 minutes) getFastestRoute(roadMap, "Lakeview", "Bakery"); // [Lakeview, Market Square, Riverside, Bakery] // (2 + 1 + 2 = 5 minutes) getFastestRoute(roadMap, "Bakery", "Hilltop"); // null (no roads go to Hilltop)

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 method 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.

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((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, fastestTimes, howWeGotHere, 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.

  1. Your driver wants an ETA, too. Change your method to return how many minutes the fastest route takes, along with the route itself. Do you need to add up the roads along the route to get it?
  2. Say you're sending the van out to lots of different customers from the bakery. How could you find the fastest route from the bakery to every neighborhood with just one search?
  3. Suppose some roads had negative times. (Maybe each road has a cost instead of a time, and some roads pass a customer who pays you to drop off a cake on the way.) Find a road map where our algorithm gives the wrong answer. The Bellman-Ford algorithm can handle negative weights in O(NM) time. How does it work, and what should it do if there's a loop of roads whose total time is negative?
  4. Say you also know where each neighborhood is, so you can estimate how far each one is from the destination as the crow flies. How could you use that estimate to steer the search toward the destination and finalize fewer neighborhoods along the way? (Look up the A* search algorithm.)

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

. . .