SOARDO
Software Engineer (SDE)dsaintermediate3 interactive MCQs in app

Fast & Slow Pointers / Cycle Detection

O(1) space cycle detection invariantnull guard while fast and fast.nextfinding middle node on even vs odd lengths

Floyd’s tortoise-and-hare moves `slow` by 1 step and `fast` by 2 steps each iteration, achieving O(n) time and O(1) extra space (vs O(n) space for a `visited` set). Two classic uses: (1) Cycle detection — if a cycle exists, `fast` laps `slow` inside the loop so `slow === fast` cleanly; (2) Finding the middle node — when `fast` reaches the end (`not fast or not fast.next`), `slow` sits at the middle.

on `1 -> 2 -> 3 -> 4 -> 5`, `(slow, fast)` steps `(1,1) -> (2,3) -> (3,5)`—`fast.next` is `None`, so `slow` sits right at middle node `3`!
checking `fast.next` without checking `fast` first (`while fast and fast.next:`), causing `AttributeError` / `NullPointerException` on even-length non-cyclic lists.
Floyd Cycle Detection — GeeksforGeeksread ↗Fast & Slow Pointers / Cycle Detection — Applied Practicepractice ↗VisuAlgo Interactive Visualizationsvisualize ↗NeetCode DSA Roadmappractice ↗

Practice Fast & Slow Pointers / Cycle Detection with 3 graded MCQs & written feedback.

Take SOARDO’s free 12-question diagnostic to see how strong you are across your target role and unlock adaptive daily missions.

Test yourself free on SOARDO →View full Software Engineer roadmap

More Software Engineer Study Guides