Shorter Distance Around a Ring of Bus Stops

Quick Overview

Shorter Distance Around a Ring of Bus Stops. Stops0 through n-1 form a circle. distance[i] is the nonnegative distance from stop i to (i+1)%n, traversable in either direction.

Shorter Distance Around a Ring of Bus Stops

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Stops 0 through n-1 form a circle. distance[i] is the nonnegative distance from stop i to `(i+1)%n`, traversable in either direction. Return the shortest distance from start to destination. ```cpp class Solution { public: int distanceBetweenBusStops(vector<int>& distance, int start, int destination); }; ``` ### Examples **Example 1:** ```text Input: distance = [2,7,1,4], start = 1, destination = 3 Output: 6 Explanation: One arc has length7+1=8; the other has2+4=6. ``` **Example 2:** ```text Input: distance = [5,0,2], start = 1, destination = 2 Output: 0 Explanation: The direct segment has zero length, which is allowed. ``` ### Constraints - The ring has n stops with `1 <= n <= 10000`, and distance has length n. - start and destination are labels in `[0,n-1]`. - Every segment length is an integer in `[0,10000]`. ### Contract considerations The distance array describes undirected travel along neighboring stops, not straight-line geometry. ```hint Read the boundary condition What result is required when the stop labels coincide, or when a connecting segment has zero length? ``` ### Boundary reminders The distance array describes undirected travel along neighboring stops, not straight-line geometry.

Overview: Shorter Distance Around a Ring of Bus Stops. Stops0 through n-1 form a circle. distance[i] is the nonnegative distance from stop i to (i+1)%n, traversable in either direction.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Stops 0 through n-1 form a circle. distance[i] is the nonnegative distance from stop i to (i+1)%n, traversable in either direction. Return the shortest distance from start to destination.

class Solution {
public:
    int distanceBetweenBusStops(vector<int>& distance, int start, int destination);
};

Examples

Example 1:

Input: distance = [2,7,1,4], start = 1, destination = 3
Output: 6
Explanation: One arc has length7+1=8; the other has2+4=6.

Example 2:

Input: distance = [5,0,2], start = 1, destination = 2
Output: 0
Explanation: The direct segment has zero length, which is allowed.

Constraints

  • The ring has n stops with 1 <= n <= 10000 , and distance has length n.
  • start and destination are labels in [0,n-1] .
  • Every segment length is an integer in [0,10000] .

Contract considerations

The distance array describes undirected travel along neighboring stops, not straight-line geometry.

Boundary reminders

The distance array describes undirected travel along neighboring stops, not straight-line geometry.