Reported September 2026
Amazonarray

Fare Between Stops on a Train Route

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

Amazon reported this one in September 2026, and it looks too easy to be real. A route of up to 100000 unique stops, two names, and you return how many segments apart they are. The catch is that the input is big enough to punish sloppy scanning if a test harness calls it repeatedly, and the -1 case is where people lose points. It's an array and hash-table problem dressed up as a train map. If you blank on the setup during the live OA, StealthCoder runs invisibly as a safety net, but you probably won't need it for this.

The problem

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.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that fare equals the absolute difference between the two stop indices. Stops are unique, so each name maps to exactly one position. Walk the array once, record the index of start and the index of stop, then return abs(i - j). If either is never found, return -1. That's O(n) time and O(1) extra space, which is fine for 100000 entries. A hash map from name to index also works and pays off if you'd answer many queries, but for a single call it's extra memory for nothing. Common pitfalls: assuming start is before stop, forgetting the reverse direction, returning 0 when a stop is missing, and the case where start equals stop, which should give 0 if present. Don't build a path or simulate travel. If you freeze during the live OA, StealthCoder is the hedge that hands you this solution fast.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Fare Between Stops on a Train Route cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Fare Between Stops on a Train Route FAQ

How hard is the Amazon train fare question really?+

Easy. It's an index lookup problem with a missing-value check. The whole solution is a single pass over the route and an absolute difference. Most candidates lose points on edge cases, not the algorithm, so spend your time on those.

What's the trick to calculateFare?+

Fare is abs(indexOf(start) - indexOf(stop)). Stops are unique, so each name has one position. Direction doesn't matter because the cost is symmetric. Return -1 if either name never shows up in the route.

Do I need a hash map for this?+

Not for one call. A single linear scan that records both indices is O(n) with O(1) space. A hash map helps only if you'd run many queries against the same route. For this signature, the scan is simpler and just as fast.

What edge cases should I test?+

Test start equal to stop (should return 0 if present), either stop missing, a route of length 1, reversed order like Example 2, and stops at the very first and last positions. Also confirm you don't return a negative fare from subtracting in the wrong order.

How do I prepare for this in 48 hours?+

Practice writing index-finding loops and early exits cleanly in your language. Rehearse the missing-value path and the absolute difference. This question tests care, not cleverness, so a quick dry run of the three examples is enough prep.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it