Data Structures & AlgorithmsHardCodeforces Div2 B · DP warm-up

Problem: Floyd's cycle detection ('tortoise and hare') finds whether a linked list has a cycle. What are its time and space complexity?

  1. A. O(log n) time, O(1) space
  2. B. O(n) time, O(n) space (needs a visited-set)
  3. C. O(n^2) time, O(1) space
  4. D. O(n) time, O(1) space

Correct answer: D. O(n) time, O(1) space

Two pointers moving at different speeds are guaranteed to meet inside a cycle within O(n) steps, using only two pointer variables -- no extra data structure, so O(1) space.

More Coding Interview Problems questions

Exams that ask this topic