Detect cycle in linked list using Floyd algorithm
Reported in HelloFresh European engineering loops. Classic linked list pointer question in entry and mid-level interviews.
Interview scenario
Context for HelloFresh candidates:
Given the head of a linked list, determine whether it contains a cycle.
Model answer
Try answering aloud first
Cover trade-offs, structure, and a concrete example before revealing the baseline response.
How to frame this at HelloFresh: Connect your answer to measurable impact, clarity of thought, and trade-offs the team cares about. Below is a strong baseline response you can adapt with your own project examples.
Use two pointers: slow moves one step, fast moves two steps. If there is a cycle, fast eventually laps slow and both pointers meet; if fast reaches null, there is no cycle.
This approach avoids additional hash memory and runs in O(n) time and O(1) space. Interviewers also like a follow-up: after meeting, reset one pointer to head and move both one step to find the cycle start.
Discussion
Comments (0)
Share how this question came up in your loop, or add tips for others preparing.
Log in to comment on this question.