Detect a Linked-List Cycle
Reported by candidates from Capgemini's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Capgemini OA, reported in September 2026, is scanning the whole array instead of walking the chain from node 0. The problem hands you a linked list encoded as nextIndex, where -1 ends the list, and asks if a cycle is reachable. It's the classic cycle detection question dressed up as an array. Up to 200000 nodes, so nothing exotic, but sloppy code will loop forever or miss a case. If you blank during the live assessment, StealthCoder runs invisibly as a safety net and gives you the solution on screen. Know the trick anyway. It takes ten lines.
The problem
A linked list is encoded by nextIndex: node i points to node nextIndex[i], and -1 marks the end. Traversal starts at node 0. Return whether the reachable chain contains a cycle. An empty array represents an empty list. Function hasCycle(nextIndex: int[]) → boolean Examples Example 1 nextIndex = [-1] return = false Case 1 exercises the documented deterministic contract. Example 2 nextIndex = [1,2,-1] return = false Case 2 exercises the documented deterministic contract. Example 3 nextIndex = [1,2,0] return = true Case 3 exercises the documented deterministic contract. Constraints 0 <= nextIndex.length <= 200000. Every value is -1 or a valid node index.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Only the chain reachable from node 0 matters. A cycle sitting in unreachable indices doesn't count, so a loop over every index with a visited check can return a wrong true. Two clean approaches work. First, Floyd's tortoise and hare: move slow one step, fast two steps, stop when either hits -1 (false) or they meet (true). That's O(n) time and O(1) space. Second, a visited boolean array: walk from 0, mark each node, return true if you land on a marked one. Both are fine at 200000. Edge cases: an empty array returns false immediately, and [-1] returns false. When advancing fast, check that the first step isn't -1 before taking the second, or you'll index with -1 and crash. StealthCoder is the hedge if the pointer bookkeeping slips under pressure live.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Detect a Linked-List Cycle cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as linked list cycle. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Capgemini's OA.
Capgemini reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Detect a Linked-List Cycle FAQ
What's the trick in the Capgemini linked-list cycle problem?+
Start at node 0 and follow nextIndex until you hit -1 or revisit a node. Use Floyd's two pointers or a visited array. Don't check cycles anywhere unreachable from node 0, because those shouldn't count toward the answer.
How hard is this OA question really?+
Easy. It's the standard cycle detection pattern with the list stored as an array of indices. The only real risks are the empty array, the -1 terminator, and indexing with -1 when fast pointer jumps twice.
Should I use Floyd's algorithm or a visited array?+
Either passes at 200000 nodes. Floyd uses O(1) memory and is the cleaner answer if asked. A visited array is harder to get wrong under pressure. Pick the one you can write without bugs in five minutes.
What edge cases should I test?+
Test the empty array (false), [-1] (false), a self-loop like [0] (true), a full cycle like [1,2,0] (true), and a chain with a cycle not reachable from node 0. The examples cover only some of these, so write your own.
How do I prepare for this in 48 hours?+
Write Floyd's cycle detection from memory twice, once on a real linked list and once on an index array. Then run the edge cases above. That's enough. This pattern is short and repeats across many linked-list and graph questions.