Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 the stops number i and (i + 1) % n.
The bus goes along both directions i.e. clockwise and counterclockwise.
Return the shortest distance between the given start and destination stops.
Example 1:
Input: distance = [1,2,3,4], start = 0, destination = 1 Output: 1 Explanation: Distance between 0 and 1 is 1 or 9, minimum is 1.
Example 2:
Input: distance = [1,2,3,4], start = 0, destination = 2 Output: 3 Explanation: Distance between 0 and 2 is 3 or 7, minimum is 3.
Example 3:
Input: distance = [1,2,3,4], start = 0, destination = 3 Output: 4 Explanation: Distance between 0 and 3 is 6 or 4, minimum is 4.
Constraints:
1 <= n <= 10^4distance.length == n0 <= start, destination < n0 <= distance[i] <= 10^4Problem summary: 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 the stops number i and (i + 1) % n. The bus goes along both directions i.e. clockwise and counterclockwise. Return the shortest distance between the given start and destination stops.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,2,3,4] 0 1
[1,2,3,4] 0 2
[1,2,3,4] 0 3
minimum-costs-using-the-train-line)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1184: Distance Between Bus Stops
class Solution {
public int distanceBetweenBusStops(int[] distance, int start, int destination) {
int s = Arrays.stream(distance).sum();
int n = distance.length, t = 0;
while (start != destination) {
t += distance[start];
start = (start + 1) % n;
}
return Math.min(t, s - t);
}
}
// Accepted solution for LeetCode #1184: Distance Between Bus Stops
func distanceBetweenBusStops(distance []int, start int, destination int) int {
s, t := 0, 0
for _, x := range distance {
s += x
}
for start != destination {
t += distance[start]
start = (start + 1) % len(distance)
}
return min(t, s-t)
}
# Accepted solution for LeetCode #1184: Distance Between Bus Stops
class Solution:
def distanceBetweenBusStops(
self, distance: List[int], start: int, destination: int
) -> int:
s = sum(distance)
t, n = 0, len(distance)
while start != destination:
t += distance[start]
start = (start + 1) % n
return min(t, s - t)
// Accepted solution for LeetCode #1184: Distance Between Bus Stops
impl Solution {
pub fn distance_between_bus_stops(distance: Vec<i32>, start: i32, destination: i32) -> i32 {
let s: i32 = distance.iter().sum();
let mut t = 0;
let n = distance.len();
let mut start = start as usize;
let destination = destination as usize;
while start != destination {
t += distance[start];
start = (start + 1) % n;
}
t.min(s - t)
}
}
// Accepted solution for LeetCode #1184: Distance Between Bus Stops
function distanceBetweenBusStops(distance: number[], start: number, destination: number): number {
const s = distance.reduce((a, b) => a + b, 0);
const n = distance.length;
let t = 0;
while (start !== destination) {
t += distance[start];
start = (start + 1) % n;
}
return Math.min(t, s - t);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.