FastPrepFare Between Stops on a Train Route
Problems/Fare Between Stops on a Train Route

Fare Between Stops on a Train Route

Amazon logoAmazon● EasyFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Amazon SDE2 interview list as a unit-fare query on the route G-U-H-K-I. The source does not specify a reusable input shape, reverse travel, missing-stop behavior, or bounds; this practice version accepts an array of unique stop names, permits travel in either direction, returns -1 when either stop is missing, and caps the route at 100,000 printable-ASCII stop names. Problem match confidence: approximately 92%.

A train route is given as an ordered array of unique stop names. Traveling across one adjacent segment costs one fare unit in either direction.

Implement calculateFare to return the fare between start and stop. Return -1 if either requested stop is absent.

Function

calculateFare(route: String[], start: String, stop: String) → int

Examples

Example 1

route = ["G","U","H","K","I"]start = "G"stop = "I"return = 4

The endpoints are four adjacent segments apart.

Example 2

route = ["A","B","C"]start = "C"stop = "A"return = 2

Travel in the reverse direction costs the same two units.

Example 3

route = ["A","B"]start = "A"stop = "Z"return = -1

Z is not a stop on the route.

Constraints

  • 1 ≤ route.length ≤ 100000.
  • Every route entry is a non-empty unique printable ASCII string.
  • start and stop are non-empty printable ASCII strings.

More Amazon problems

  • Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
See Amazon hiring insights
Saved
class Solution {
    public int calculateFare(String[] route, String start, String stop) {
        // write your code here
    }
}
Checking account…