Minimum Moves Around a Circular Array
Updated
Problem statement
A circular array has n positions indexed from 0 to n - 1. You start at index startIndex and want to reach index targetIndex.
In one move, you may go to either neighboring position:
- Move right from index
ito(i + 1) % n. - Move left from index
ito(i - 1 + n) % n.
Return the minimum number of moves needed to reach targetIndex. If you already start at the target, return 0.
Only positions matter; the contents of the array do not affect movement.
Function
minimumCircularMoves(n: int, startIndex: int, targetIndex: int) → intExamples
Example 1
n = 5startIndex = 0targetIndex = 4return = 1One left move wraps directly from index 0 to index 4. Moving right would take four moves.
Example 2
n = 7startIndex = 2targetIndex = 5return = 3The shortest route is 2 → 3 → 4 → 5, which takes three moves. The other direction takes four moves.
Example 3
n = 1startIndex = 0targetIndex = 0return = 0The starting position is already the target, so no move is needed.
Constraints
1 <= n <= 10^90 <= startIndex < n0 <= targetIndex < n
More Vanguard Online Assessment questions
- Two SumOA · Seen Sep 2026
- Valid ParenthesesOA · Seen Sep 2026
- Are They PangramsOA · Seen Nov 2025
- Missing WordsOA · Seen Nov 2025
- Spam DetectionOA · Seen Nov 2025
- Binge WatchingOA · Seen Jan 2023
- Maximum Occurring CharacterOA · Seen Mar 2022
- Picking TicketsOA · Seen Mar 2022
- Tool List: Two Smallest ValuesOA · Seen Nov 2021
- Group AnagramsOA · Seen Nov 2019
Similar questions at other companies
- Count Valid Square PairsAuxoAI · OA
- Picked Player Who LostSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Server InvestmentRippling · OARippling interview questions
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Next Pascal Row CoefficientIntuit · OAIntuit interview questions
- Unique Integers That Sum to ZeroNVIDIA · OANVIDIA interview questions
- Count Multiples of Three with Two SevensHudson River Trading · OAHudson River Trading interview questions
- Diagonal Bounce TraversalAutodesk · OAAutodesk interview questions
- Maximum Student-Teacher DistanceChubb · OAChubb interview questions
- Maximum Three-Server Difficulty ScoreVisa · OAVisa interview questions