Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Shortest Path for Delivery Batch

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Shortest Path for Delivery Batch. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

A DoorDash delivery batch is represented by a weighted directed graph. Each vertex is a location, and each edge contains the travel time between two locations. Starting from a given location, find the minimum-time route that visits every required delivery location in any order.

Return the route as an ordered list of locations and its total travel time. If no route can visit all delivery locations, return ([], -1).

Formal Specification

Implement shortest_batch_route(graph, start, deliveries).

  • graph is a dictionary mapping each location to a list of [neighbor, travel_time] pairs.
  • start is a location identifier present in the graph.
  • deliveries is a list of distinct location identifiers.
  • Travel times are nonnegative integers.
  • Return [route, total_time], where route starts at start and contains every delivery location exactly at least once. Intermediate locations may repeat.

The route must be globally optimal. It is not enough to choose the nearest undelivered stop at each step.

Constraints

  • 1 <= number of locations <= 200
  • 0 <= number of directed edges <= 2,000
  • 1 <= len(deliveries) <= 12
  • Delivery location identifiers are distinct and appear in graph
  • Edge travel times are integers in the range 0 through 10^6
  • All edge weights are nonnegative

Function Signature

def shortest_batch_route(graph, start, deliveries):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output