Fast & Slow Pointers

Fundamentals

Cycle detection and midpoint traversal using Floyd's Tortoise and Hare algorithm

7 problems·4 Easy·3 Medium·0 Hard
Pattern Study Guide & Cheat Sheet
▼

Advance two pointers at different speeds (usually 1 step and 2 steps) to detect cycles or locate the middle node in O(N) time and O(1) space.

Core Invariant: In a cycle of length C, each iteration closes the gap between fast and slow by 1. Fast catches slow in <= C steps. Distance from head to cycle entry equals distance from collision point to cycle entry.

Recognize it (Keywords & Signals)
  • Detect cycle in a Linked List
  • Find the exact start / entry node of a cycle
  • Find middle node of a Linked List in a single pass
  • Find duplicate number in array without modifying it (Floyd cycle)
  • Happy Number (detect repeating loop in sum of squared digits)
When NOT to use

Need random access or already have O(N) auxiliary space permitted to use a Hash Set.

How to solve (Step-by-step)
  1. 1.Initialize `slow = head`, `fast = head`.
  2. 2.Advance `slow = slow.next` and `fast = fast.next.next` inside `while fast and fast.next`.
  3. 3.If `slow == fast`, a cycle is detected.
  4. 4.To find cycle start: reset `slow = head`. Move both `slow` and `fast` 1 step at a time until they meet. The meeting node is the cycle entry.
  5. 5.For middle node: when fast reaches the end, slow is at the middle.
Watch for (Interview Traps)
  • Dereferencing `fast.next.next` without checking both `fast` and `fast.next` (causes AttributeError)
  • Even-length lists: slow stops at the second middle node if starting both at head
  • Assuming an array has no cycle without checking bounds when modeling indices as pointers
Floyd's Cycle Detection & Entry Finder
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def detect_cycle_entry(head: ListNode | None) -> ListNode | None:
    slow = fast = head
    
    # Phase 1: Determine if a cycle exists
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
        if slow == fast:
            break
    else:
        return None  # Reached end of list -> No cycle
        
    # Phase 2: Find cycle entry node
    slow = head
    while slow != fast:
        slow = slow.next
        fast = fast.next
        
    return slow  # Both pointers meet at cycle entrance
Cost
O(n) linear scan · O(1) strictly constant space (Guarantees O(1) space where a visited hash set would take O(n).)
Canonical problems
#141 Linked List Cycle: Verify if fast pointer collides with slow pointer
#142 Linked List Cycle II: Reset slow to head to find exact entry node
#876 Middle of the Linked List: When fast hits the end, slow is at the midpoint
#287 Find the Duplicate Number: Treat array indices as pointers and find cycle entrance
Easy·31 companies·Max freq 100%·Acc 59.9%
NikeSnowflakeJump Trading+28
Easy·28 companies·Max freq 100%·Acc 54.7%
YahooNeetCode 150Blind 75+25
Medium·24 companies·Max freq 90%·Acc 64.6%
NianticNikeNeetCode 150+21
Easy·18 companies·Max freq 93%·Acc 58.3%
IXLAMDIntel+15
Medium·14 companies·Max freq 79%·Acc 58.5%
PaytmVMwareAmerican Express+11
Easy·13 companies·Max freq 88%·Acc 82.1%
Walmart LabsAMDAccenture+10
Medium·5 companies·Max freq 38%·Acc 37.8%
Goldman SachsAmazonGoogle+2
7 displayed (of 7 matching)Filtered: All Companies

Top Companies Testing Fast & Slow Pointers

Browse company-specific interview problem lists and recency breakdowns:

Explore All 22 DSA Coding Patterns

Master core algorithmic patterns tested in FAANG & Big Tech coding interviews.

Patterns Hub

Fast & Slow Pointers frequently asked questions

What is the Fast & Slow Pointers pattern in coding interviews?

Cycle detection and midpoint traversal using Floyd's Tortoise and Hare algorithm It is categorized under Fundamentals and features 7 curated interview problems across Easy (4), Medium (3), and Hard (0) difficulties.

Which tech companies frequently test Fast & Slow Pointers?

Companies frequently asking Fast & Slow Pointers questions include Microsoft, Google, Amazon, Bloomberg, NeetCode 150, tcs.

How do I master the Fast & Slow Pointers pattern?

Start by studying the algorithmic strategy and template code, then practice Easy and Medium problems before attempting Hard problems under timed conditions.