Detect a Linked-List Cycle
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google flagged this one in April 2026, and the trap is the self-loop. A list where next = [0] looks like a single harmless node, but the head points straight back at itself. This is cycle detection on an array-encoded linked list, and the edge cases do the damage: empty input, a -1 that ends the walk, and entries nobody can reach from node 0. If your first instinct is to scan the whole array, you'll get it wrong. If you blank on the traversal logic during the live OA, StealthCoder is the invisible safety net that reads the problem and hands you a working answer.
The problem
A singly linked list is serialized by an integer array next. Node i points to node next[i]; a value of -1 means null. When the array is non-empty, node 0 is the head. Entries that are not reachable from node 0 are ignored. Return true if following next pointers from the head eventually revisits a node. Return false for an empty list or when traversal reaches null. Function hasLinkedListCycle(next: int[]) → boolean Examples Example 1 next = [1,2,3,1] return = true Traversal from node 0 enters the cycle 1 -> 2 -> 3 -> 1. Example 2 next = [1,2,3,-1] return = false Traversal reaches node 3 and then null. Example 3 next = [0] return = true The head points to itself. Example 4 next = [] return = false The empty array represents an empty list. Constraints 0 <= next.length <= 200000. For every index i, next[i] == -1 or 0 <= next[i] < next.length.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Start at node 0 and follow next pointers. Two clean ways to do it. Option one: a visited boolean array. Mark each node as you step on it, and if you land on one already marked, return true. If you hit -1, return false. Option two: Floyd's tortoise and hare, which uses O(1) extra space. Slow moves one step, fast moves two, and you stop when fast hits -1 or they meet. The common pitfall is checking every index for a cycle. The problem says unreachable entries are ignored, so a cycle sitting off to the side doesn't count. The other pitfall is forgetting the empty array, where next[0] doesn't exist. Guard that first. With up to 200000 nodes, both approaches run in O(n). If the pointer logic slips under pressure, StealthCoder can cover you during the live OA.
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 Google's OA.
Google 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 to the Google linked-list cycle OA question?+
Only walk from node 0. Entries unreachable from the head are ignored, so scanning every index will give false positives. Follow next pointers, track visited nodes or use two pointers, and stop when you hit -1 or revisit a node.
Do I need Floyd's algorithm or is a visited array fine?+
A visited array is fine. With length up to 200000, O(n) memory is no problem. Floyd's saves space and is a nice mention, but correctness matters more. Pick the one you can write without bugs in one pass.
Which edge cases break a naive solution here?+
The empty array, which must return false before you touch index 0. The self-loop like [0], which must return true. And a cycle in unreachable nodes, which must be ignored. Also check that -1 ends traversal instead of being used as an index.
How hard is this really?+
Easy if you know cycle detection. It's the classic linked-list cycle problem dressed up with an array encoding. The array format trips people who expect node objects, but the logic is the same. Most of the risk is in the edge cases.
How do I prepare for this in 48 hours?+
Write the visited-array version and the fast/slow pointer version from scratch once each. Then test the four examples: cycle, null end, self-loop, and empty. Add one case with an unreachable cycle. That covers nearly everything this question can throw at you.