Shortest distance between two stops on a circular bus route

Quick Overview

Given the distances between neighboring stops on a circular bus route, find the shortest distance between two stops when the bus can travel in either direction. It tests array summation over a range, reasoning about circular indexing, and care with off-by-one edges and the case where the two stops are the same.

Shortest distance between two stops on a circular bus route

Company: Box

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

A bus has `n` stops numbered from `0` to `n - 1` that form a circle. We know the distance between all pairs of neighboring stops, where `distance[i]` is the distance between stop `i` and stop `(i + 1) % n`. Return the shortest distance between the given `start` and `destination` stops. ### Function Signature ```python def distance_between_bus_stops(distance: list[int], start: int, destination: int) -> int: ``` ### Rules - The bus can travel in either direction around the circle. - `start` may be smaller than, larger than or equal to `destination`. When the two stops are the same, the distance is `0`. ### Constraints - `1 <= n <= 10^4` - `distance.length == n` - `0 <= distance[i] <= 10^4` - `0 <= start, destination < n` ### Examples ```text Input: distance = [1,2,3,4], start = 0, destination = 1 Output: 1 ``` The clockwise distance from stop 0 to stop 1 is 1. The counterclockwise distance is 2 + 3 + 4 = 9, so the shortest distance is 1. ```text Input: distance = [1,2,3,4], start = 0, destination = 2 Output: 3 ``` The clockwise distance from stop 0 to stop 2 is 1 + 2 = 3. The counterclockwise distance is 3 + 4 = 7, so the shortest distance is 3. ```text Input: distance = [1,2,3,4], start = 3, destination = 0 Output: 4 ``` Going clockwise, stop 3 reaches stop 0 in one step of length `distance[3] = 4`. The other way passes stops 2 and 1 for 3 + 2 + 1 = 6, so the shortest distance is 4.

Overview: Given the distances between neighboring stops on a circular bus route, find the shortest distance between two stops when the bus can travel in either direction. It tests array summation over a range, reasoning about circular indexing, and care with off-by-one edges and the case where the two stops are the same.

Box logo
Box
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
1
0

A bus has n stops numbered from 0 to n - 1 that form a circle. We know the distance between all pairs of neighboring stops, where distance[i] is the distance between stop i and stop (i + 1) % n.

Return the shortest distance between the given start and destination stops.

Function Signature

def distance_between_bus_stops(distance: list[int], start: int, destination: int) -> int:

Rules

  • The bus can travel in either direction around the circle.
  • start may be smaller than, larger than or equal to destination . When the two stops are the same, the distance is 0 .

Constraints

  • 1 <= n <= 10^4
  • distance.length == n
  • 0 <= distance[i] <= 10^4
  • 0 <= start, destination < n

Examples

Input: distance = [1,2,3,4], start = 0, destination = 1
Output: 1

The clockwise distance from stop 0 to stop 1 is 1. The counterclockwise distance is 2 + 3 + 4 = 9, so the shortest distance is 1.

Input: distance = [1,2,3,4], start = 0, destination = 2
Output: 3

The clockwise distance from stop 0 to stop 2 is 1 + 2 = 3. The counterclockwise distance is 3 + 4 = 7, so the shortest distance is 3.

Input: distance = [1,2,3,4], start = 3, destination = 0
Output: 4

Going clockwise, stop 3 reaches stop 0 in one step of length distance[3] = 4. The other way passes stops 2 and 1 for 3 + 2 + 1 = 6, so the shortest distance is 4.