Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
PROBLEM-SOLVING TECHNIQUES

Fast & Slow Pointers

Two pointers at different speeds to find cycles and midpoints.

Intermediate7 problems2 interactive subtopicsO(n) time · O(1) space
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (2)Open in full playground →
STEP 1/4Both start at 1
node 5 links back to 3
1
2
3
4
5
slow
fast
  • result
Space Play←→ StepR Replay
1 / 4
PYTHON1 -> 2 -> 3 -> 4 -> 5, and 5 links back to 3
1def has_cycle(head):
▶2 slow = fast = head
3 while fast and fast.next:
4 slow = slow.next
5 fast = fast.next.next
6 if slow is fast:
7 return True
8 return False
STEP 1 OF 4Setup

Both start at 1

slow will take one step at a time, fast two.

VARIABLES

slow
1
fast
1
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

A slow pointer moves one step while a fast pointer moves two. If there’s a cycle, fast eventually laps slow; if not, fast reaches the end.

Why it matters

It detects cycles and finds the middle in O(n) time with O(1) memory - no visited set needed.

Think of it like this

Two runners on a track: if it’s a loop, the faster one will lap the slower one.

Use it when

  • Detecting a cycle in a linked list
  • Finding the middle node
  • Finding where a cycle starts (Floyd’s algorithm)

Avoid it when

  • A visited set is affordable and you want simpler code
  • Arrays where indices make the answer trivial

Complexity cheatsheet

OperationTimeSpace
Cycle detectionO(n)O(1)
Visited-set alternativeO(n)O(n)
Find the middleO(n)O(1)

Often confused with: two pointers - usually compares values; fast and slow only compares positions.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

cyclelinked listmiddlehappy numberduplicate in 1..n
  • “O(1) extra space” on a linked-list cycle question means Floyd’s algorithm
  • Values in 1..n can be treated as next pointers
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • Reading fast.next.next without checking fast and fast.next first
  • Comparing values instead of node identity (use is)
  • Starting fast one step ahead and getting the middle wrong for even lengths

Edge cases to test

  • An empty list or a single node
  • A node pointing to itself
  • Even vs odd length when finding the middle
Interview tip: To find where the cycle starts: after they meet, reset one pointer to head and move both one step at a time.
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

Why must fast eventually meet slow inside a cycle?

06

Practice

Apply what you just watched on real interview problems.

Generate Linked List problem in AI DSA Practice →
  • BeginnerLinked List CycleLeetCode 141 ↗ (opens in a new tab)
  • IntermediateLinked List Cycle IILeetCode 142 ↗ (opens in a new tab)
  • AdvancedFind the Duplicate NumberLeetCode 287 ↗ (opens in a new tab)
Prefix SumBinary Search on Answer