Moda Operandi Interview Question: How do you find out if there ... | Glassdoor

Interview Question

Software Engineer Interview New York, NY

How do you find out if there is a cycle in a linked list

  ? Mention time/space complexity, try to come up with the most optimal solution (of course there are more that one)

Interview Answer

2 Answers


Use one pointer. Move forward. Mark nodes as visited as you pass them. If you bump into a visited node before you reach the end, there’s a cycle, otherwise there’s not.

Interview Candidate on Jan 10, 2019

Use 2 pointer racing, the first iterates every node ,the second jumps every other node in the list.

If second pointer reached the end of the list - no loop
if second pointer reached the first - it is loop

Anonymous on Jan 24, 2019

Add Answers or Comments

To comment on this, Sign In or Sign Up.