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.
Given an integer n and an integer array rounds. We have a circular track which consists of n sectors labeled from 1 to n. A marathon will be held on this track, the marathon consists of m rounds. The ith round starts at sector rounds[i - 1] and ends at sector rounds[i]. For example, round 1 starts at sector rounds[0] and ends at sector rounds[1]
Return an array of the most visited sectors sorted in ascending order.
Notice that you circulate the track in ascending order of sector numbers in the counter-clockwise direction (See the first example).
Example 1:
Input: n = 4, rounds = [1,3,1,2] Output: [1,2] Explanation: The marathon starts at sector 1. The order of the visited sectors is as follows: 1 --> 2 --> 3 (end of round 1) --> 4 --> 1 (end of round 2) --> 2 (end of round 3 and the marathon) We can see that both sectors 1 and 2 are visited twice and they are the most visited sectors. Sectors 3 and 4 are visited only once.
Example 2:
Input: n = 2, rounds = [2,1,2,1,2,1,2,1,2] Output: [2]
Example 3:
Input: n = 7, rounds = [1,3,5,7] Output: [1,2,3,4,5,6,7]
Constraints:
2 <= n <= 1001 <= m <= 100rounds.length == m + 11 <= rounds[i] <= nrounds[i] != rounds[i + 1] for 0 <= i < mProblem summary: Given an integer n and an integer array rounds. We have a circular track which consists of n sectors labeled from 1 to n. A marathon will be held on this track, the marathon consists of m rounds. The ith round starts at sector rounds[i - 1] and ends at sector rounds[i]. For example, round 1 starts at sector rounds[0] and ends at sector rounds[1] Return an array of the most visited sectors sorted in ascending order. Notice that you circulate the track in ascending order of sector numbers in the counter-clockwise direction (See the first example).
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
4 [1,3,1,2]
2 [2,1,2,1,2,1,2,1,2]
7 [1,3,5,7]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1560: Most Visited Sector in a Circular Track
class Solution {
public List<Integer> mostVisited(int n, int[] rounds) {
int m = rounds.length - 1;
List<Integer> ans = new ArrayList<>();
if (rounds[0] <= rounds[m]) {
for (int i = rounds[0]; i <= rounds[m]; ++i) {
ans.add(i);
}
} else {
for (int i = 1; i <= rounds[m]; ++i) {
ans.add(i);
}
for (int i = rounds[0]; i <= n; ++i) {
ans.add(i);
}
}
return ans;
}
}
// Accepted solution for LeetCode #1560: Most Visited Sector in a Circular Track
func mostVisited(n int, rounds []int) []int {
m := len(rounds) - 1
var ans []int
if rounds[0] <= rounds[m] {
for i := rounds[0]; i <= rounds[m]; i++ {
ans = append(ans, i)
}
} else {
for i := 1; i <= rounds[m]; i++ {
ans = append(ans, i)
}
for i := rounds[0]; i <= n; i++ {
ans = append(ans, i)
}
}
return ans
}
# Accepted solution for LeetCode #1560: Most Visited Sector in a Circular Track
class Solution:
def mostVisited(self, n: int, rounds: List[int]) -> List[int]:
if rounds[0] <= rounds[-1]:
return list(range(rounds[0], rounds[-1] + 1))
return list(range(1, rounds[-1] + 1)) + list(range(rounds[0], n + 1))
// Accepted solution for LeetCode #1560: Most Visited Sector in a Circular Track
struct Solution;
impl Solution {
fn most_visited(n: i32, rounds: Vec<i32>) -> Vec<i32> {
let m = rounds.len();
let start = rounds[0];
let end = rounds[m - 1];
if start == end {
return vec![start];
}
if start < end {
return (start..=end).collect();
}
let mut res: Vec<i32> = (start..=end + n).map(|x| ((x - 1) % n) + 1).collect();
res.sort_unstable();
res
}
}
#[test]
fn test() {
let n = 4;
let rounds = vec![1, 3, 1, 2];
let res = vec![1, 2];
assert_eq!(Solution::most_visited(n, rounds), res);
let n = 2;
let rounds = vec![2, 1, 2, 1, 2, 1, 2, 1, 2];
let res = vec![2];
assert_eq!(Solution::most_visited(n, rounds), res);
let n = 7;
let rounds = vec![1, 3, 5, 7];
let res = vec![1, 2, 3, 4, 5, 6, 7];
assert_eq!(Solution::most_visited(n, rounds), res);
let n = 3;
let rounds = vec![3, 2, 1, 2, 1, 3, 2, 1, 2, 1, 3, 2, 3, 1];
let res = vec![1, 3];
assert_eq!(Solution::most_visited(n, rounds), res);
}
// Accepted solution for LeetCode #1560: Most Visited Sector in a Circular Track
function mostVisited(n: number, rounds: number[]): number[] {
const ans: number[] = [];
const m = rounds.length - 1;
if (rounds[0] <= rounds[m]) {
for (let i = rounds[0]; i <= rounds[m]; ++i) {
ans.push(i);
}
} else {
for (let i = 1; i <= rounds[m]; ++i) {
ans.push(i);
}
for (let i = rounds[0]; i <= n; ++i) {
ans.push(i);
}
}
return ans;
}
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.