Circular Array Loop
Problem Statement
You are given a circular array nums of non-zero integers. Standing at index i, you jump nums[i] places: forward if it is positive, backward if it is negative. Because the array is circular, going past the last index wraps to the start, and going before index 0 wraps to the end.
Return true if the array contains a valid cycle: a sequence of jumps that comes back to where it started, where
- every index in the cycle has the same sign (all jumps forward, or all jumps backward), and
- the cycle has more than one index (an index that jumps onto itself does not count).
Otherwise return false.
An integer array nums with no zeros.
true if a valid cycle exists, otherwise false.
0 → 2 → 3 → 0. All three jumps are forward and the cycle has 3 indices.
Index 5 jumps 6 places and lands on itself — a cycle of length 1, which does not count. Every other path ends up mixing backward and forward jumps.
0 → 1 → 0 mixes directions, so it is not valid. But 3 → 4 → 3 (jump 1, then jump 4 which wraps) is all forward with length 2.
- • 1 ≤ nums.length ≤ 5000
- • −1000 ≤ nums[i] ≤ 1000
- • nums[i] ≠ 0
- • Follow-up: can you solve it in O(N) time and O(1) extra space?
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
From each index, take n same-direction jumps; check the loop you are in
Floyd with direction/self-loop checks; set failed paths to 0 so each index is walked once
For every start, keep jumping. If any jump lands on an index with the other sign, this start fails. If we survive n jumps, we must already be inside the loop this walk ends in (a path can't visit more than n different indices). Then the loop is valid unless it is a single index jumping onto itself, which we check with one more next.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Linked List — Fast & Slow translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.