Reported July 2026
Amazonprefix sum

Shortest Distance on a Circular Bus 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

Stop 0 to stop 2 on a four-stop loop, and the answer is 3 instead of 7. That's the whole idea behind this Amazon OA question, reported in July 2026. A circular bus route, an array of gaps between neighboring stops, and you return the shorter way around. It's an array and prefix-sum problem dressed up as a route planner. It looks trivial, and it mostly is, but the edge cases around wraparound and start greater than destination trip people who rush. If you blank on the setup during the live assessment, StealthCoder runs invisibly as a safety net and hands you the clean solution.

The problem

For this exercise, assume a bus route has n stops arranged in a circle. The array distance contains the distance from stop i to stop (i + 1) mod n.
Given two distinct stops, start and destination, return the shorter travel distance between them. A bus may travel clockwise or counterclockwise around the circle.

Function
shortestBusRouteDistance(distance: int[], start: int, destination: int) → int

Examples
Example 1
distance = [1,2,3,4]
start = 0
destination = 2
return = 3
Clockwise travel from stop 0 to stop 2 costs 1 + 2 = 3. The other direction costs 4 + 3 = 7, so the answer is 3.
Example 2
distance = [7,10,1,12]
start = 1
destination = 3
return = 11
Travel through stops 1 -> 2 -> 3 costs 10 + 1 = 11. The opposite direction costs 12 + 7 = 19.

Constraints
2 <= distance.length <= 100000
1 <= distance[i] <= 10000
0 <= start, destination < distance.length
start != destination

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: you never need to simulate two directions. Sum the whole array once to get total. Then sum the distances going one way from the smaller index to the larger index, which is the sum of distance[lo] through distance[hi-1]. Call that clockwise. The other direction is total minus clockwise. Return the min of the two. That's O(n) time and O(1) space. The common pitfall is not swapping start and destination when start is larger, which gives a wrong loop or an off-by-one slice. Another is walking with modulo and looping forever or double counting. Check Example 2: start 1, destination 3 gives 10 + 1 = 11, total is 30, other side is 19. Answer 11. If you freeze on the index bounds in the Amazon OA, StealthCoder is your hedge. Otherwise it's a ten-line function.

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 Shortest Distance on a Circular Bus 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.

Shortest Distance on a Circular Bus Route FAQ

How hard is the circular bus route problem really?+

Easy. It's one pass over the array with a sum and a subtraction. The difficulty is only in the index bounds. If you can write a loop from min(start, destination) to max(start, destination) exclusive, you've solved the hard part.

What's the trick to solving it fast?+

Compute total distance once, then compute the one-direction sum between the two stops. The opposite direction is total minus that sum. Return the smaller of the two. No need to walk the circle twice or use modulo arithmetic.

What mistake do people make most often?+

Forgetting to order the two stops. If start is bigger than destination, a naive loop from start to destination reads the wrong range or skips entirely. Swap them first. The route is symmetric, so swapping doesn't change the answer.

What time complexity does Amazon expect here?+

Linear time is fine and optimal for a single query. With n up to 100000, an O(n) scan is trivial. Use O(1) extra space. A prefix-sum array also works but it's unnecessary for one call.

How do I prepare for this in 48 hours?+

Write it once from scratch and test both examples by hand. Then try edge cases like two stops, start greater than destination, and adjacent stops across the wrap. Thirty minutes is enough. Related prefix-sum and circular array problems are worth a quick skim.

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