The Fast & Slow Pointers Pattern (also called Floydβs Cycle Detection) uses two pointers moving at different speeds β usually one moves 1 step at a time (slow) and the other moves 2 steps at a time (fast).
If they ever meet, a cycle exists.
If the fast pointer reaches the end, thereβs no cycle.
π When to Use Fast & Slow Pointers?
- Detect cycles in linked lists
- Find starting point of a cycle
- Find middle element of a linked list
- Solve number problems that repeat (e.g., Happy Number)
Amazon tests this because itβs space-efficient (O(1) space) and avoids hashing.
π Problem 1: Detect Cycle in Linked List
π Amazon-style phrasing:
Given the head of a linked list, determine if the list has a cycle.
Java Solution
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public class LinkedListCycle {
public static boolean hasCycle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) return true; // cycle detected
}
return false;
}
public static void main(String[] args) {
ListNode head = new ListNode(3);
head.next = new ListNode(2);
head.next.next = new ListNode(0);
head.next.next.next = new ListNode(-4);
head.next.next.next.next = head.next; // cycle
System.out.println(hasCycle(head)); // Output: true
}
}
β
Time Complexity: O(n)
β
Space Complexity: O(1)
π Problem 2: Find Start of Cycle
π Amazon-style phrasing:
Modify the previous problem β if a cycle exists, return the node where the cycle begins.
Java Solution
public class LinkedListCycleStart {
public static ListNode detectCycle(ListNode head) {
ListNode slow = head, fast = head;
// Step 1: Detect cycle
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) break;
}
// No cycle
if (fast == null || fast.next == null) return null;
// Step 2: Move one pointer to head
slow = head;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return slow; // start of cycle
}
}
β Key Amazon insight: This tests if you know the math behind Floydβs algorithm β when slow and fast meet, moving one to head and advancing both by one step will give the cycle start.
π Problem 3: Find Middle of Linked List
π Amazon-style phrasing:
Given a linked list, return the middle node. If there are two middle nodes, return the second one.
Java Solution
public class MiddleOfLinkedList {
public static ListNode middleNode(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
}
β
Why Amazon likes this:
Shows if you can use pointer speed differences beyond just cycle detection.
π Problem 4: Happy Number
π Amazon-style phrasing:
Write an algorithm to determine if a number is a happy number. A happy number is a number that eventually reaches 1 after repeatedly replacing it with the sum of the squares of its digits. If it loops forever, itβs not happy.
Java Solution
public class HappyNumber {
private static int nextNumber(int n) {
int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += digit * digit;
n /= 10;
}
return sum;
}
public static boolean isHappy(int n) {
int slow = n, fast = n;
do {
slow = nextNumber(slow);
fast = nextNumber(nextNumber(fast));
} while (slow != fast);
return slow == 1;
}
public static void main(String[] args) {
System.out.println(isHappy(19)); // Output: true
System.out.println(isHappy(2)); // Output: false
}
}
β
Time Complexity: O(log n) per step
β
Why Amazon asks: This disguises cycle detection in a mathy problem.
π Extended Problem List (Practice)
- Linked List Cycle II (LeetCode 142) β Find start of cycle (covered above).
- Reorder List β Find middle + reverse second half + merge.
- Palindrome Linked List β Find middle + reverse + compare halves.
- Detect Circular Array Loop β Variation of cycle detection in arrays.
- Tortoise & Hare on Graphs β Extension to detect loops in graph traversal.
π― Key Takeaways
- Fast & Slow pointers = detect patterns by speed difference.
- Always O(1) space β Amazon loves memory-efficient solutions.
- Works in linked lists, arrays, and math problems.
π
Next in the series (Day 4):
π Merge Intervals Pattern β Amazonβs favorite for scheduling, calendar, and meeting room problems.
Top comments (0)