FastPrepCheapest Shipping Route with Carrier Details

Cheapest Shipping Route with Carrier Details

Stripe logoStripe● MediumINTERNPHONE SCREEN
Learn

Problem statement

Given shipping offers in routeData, find the cheapest route from sourceCountry to targetCountry. A route must use one offer directly, or two offers through one intermediate country.

Each comma-separated offer has the form from:to:carrier:cost, for example US:UK:UPS:4. Offers are directed and labels are case-sensitive. No reverse offer is implied. Several offers may have the same countries or carrier.

Minimize total cost. Break ties by fewer legs, then by the lexicographic sequence of country tokens, then by the lexicographic sequence of carrier tokens. Compare the individual tokens before joining them for output.

Return a JSON dictionary serialized as a string, with keys route, method and cost in that order, without extra whitespace. Country and carrier paths are colon-joined strings; cost is a decimal integer. For example: {"route":"US:UK:CA","method":"UPS:FedEx","cost":14}. If no permitted route exists, return the string {}.

The judged method is cheapestShippingRoute. Paths with more than one intermediate country are excluded.

Ungraded progression exercises

The complete editorial code also provides the following methods. They are practice follow-ups and are not called by the judge for the main method.

  • directShippingCost(routeData, sourceCountry, targetCountry, method): return the least cost among matching direct offers for that exact carrier, or -1. directShippingDetails returns its dictionary string, or {}.
  • anyShippingCost(routeData, sourceCountry, targetCountry): return a cost of one feasible direct or two-leg route, or -1. To make this exercise deterministic, scan first offers in input order. For each first offer from the source, return it immediately if it is direct; otherwise scan second offers in input order and return the first matching two-leg route. anyShippingDetails returns that same route's dictionary, or {}. This chosen order is a practice assumption.

For US:CA:Direct:9,US:UK:First:3,UK:CA:Next:4, direct lookup with carrier Direct returns 9 and {"route":"US:CA","method":"Direct","cost":9}. The feasible-route methods also return that first direct route and cost 9. The judged minimum-route method instead returns {"route":"US:UK:CA","method":"First:Next","cost":7}.

Function

cheapestShippingRoute(routeData: String, sourceCountry: String, targetCountry: String) → String

Examples

Example 1

routeData = "US:UK:UPS:4,US:UK:DHL:5,UK:CA:FedEx:10,AU:JP:DHL:20"sourceCountry = "US"targetCountry = "CA"return = "{\"route\":\"US:UK:CA\",\"method\":\"UPS:FedEx\",\"cost\":14}"

The only permitted country path is US → UK → CA. UPS then FedEx costs 4 + 10 = 14; DHL then FedEx costs 15. The compact JSON stores route US:UK:CA, methods UPS:FedEx and cost 14.

Example 2

routeData = "US:CA:Direct:9,US:UK:First:3,UK:CA:Next:4"sourceCountry = "US"targetCountry = "CA"return = "{\"route\":\"US:UK:CA\",\"method\":\"First:Next\",\"cost\":7}"

The direct route costs 9; the route through UK costs 3 + 4 = 7, so the two-leg route wins even though the direct offer appears first.

Example 3

routeData = "CA:US:Reverse:1,US:UK:A:1,UK:JP:B:1,JP:CA:C:1"sourceCountry = "US"targetCountry = "CA"return = "{}"

The reverse offer cannot be used forward. US → UK → JP → CA needs two intermediate countries and is outside the one-intermediate limit. Return the string {}.

Constraints

  • routeData contains 0 to 1000 valid offers; the empty string means no offers. Offers contain no whitespace.
  • Country tokens are 1 to 8 ASCII letters or digits; carrier tokens are 1 to 10 ASCII letters or digits. ASCII lexicographic ordering applies.
  • Each offer has different source and destination countries. The query source and target countries also differ.
  • Each cost is a base-ten integer in [0, 1000000000]. Use signed 64-bit arithmetic for costs and sums.
  • Repeated offers are permitted. Only their prices and the stated tie rules affect the minimum result. Malformed input is outside the contract.

More Stripe problems

See Stripe hiring insights
public String cheapestShippingRoute(String routeData, String sourceCountry, String targetCountry) {
    // Write your code here.
}
routeData"US:UK:UPS:4,US:UK:DHL:5,UK:CA:FedEx:10,AU:JP:DHL:20"
sourceCountry"US"
targetCountry"CA"
expected"{\"route\":\"US:UK:CA\",\"method\":\"UPS:FedEx\",\"cost\":14}"
Checking account…