DSA
    beginner

    Two Pointers Technique

    Two Pointers: Valid Palindrome

    Master the Two Pointers technique! Visualize how moving start and end pointers can check whether a string is a palindrome in linear time — one of the most common patterns for array and string problems, and a must-know for FAANG interviews.

    ⏱ O(n)
    💾 O(1)
    Ready
    L
    M
    0
    A
    1
    D
    2
    A
    3
    M
    4
    R

    Initialize pointers at start (0) and end (n-1)

    Initialize pointers at start (0) and end (n-1)

    50%
    Live Variables
    left0
    right4
    charLm
    charRm

    Palindrome check

    Sequence

    Order the steps of the two-pointer palindrome check.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    1. 1Drop a step here
    2. 2Drop a step here
    3. 3Drop a step here
    4. 4Drop a step here
    5. 5Drop a step here
    5 pieces left to place

    Test your knowledge

    20 questions
    Question 1 of 20
    Score: 0

    When is the Two Pointers technique most applicable?

    Write the code

    Runs your solution

    Valid Palindrome

    A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Implement a function to check if a string is a palindrome using the Two Pointers technique.

    Example Cases

    Input:["A man, a plan, a canal: Panama"]
    Output:true
    Input:["race a car"]
    Output:false
    Input:[" "]
    Output:true
    javascript

    The short version

    Imagine two people walking towards each other from opposite ends of a long table to meet in the middle. By scanning from both ends, they inspect the whole table much faster than one person running back and forth.

    In more depth

    Two Pointers: Core Patterns

    Two Pointers uses two integer indices to traverse a data structure, cutting O(n²) brute-force to O(n). Five distinct patterns cover the majority of interview problems.

    1. Opposite-Ends (Sorted Array)

    Left starts at 0, right at n−1. If the running value (sum, product, area) is too small advance left; too large retreat right. Requires a sorted array. Classic problems: Pair Sum, 3Sum (fix one element then run this on the rest), Container With Most Water, Trapping Rain Water.

    2. Same-Direction: Slow-Write / Fast-Read

    Both pointers move left-to-right at different rates. The fast pointer scans every element; the slow "write" pointer records the next kept position. Used for: Remove Duplicates In-Place, Move Zeroes to End.

    3. Same-Direction: Fixed Gap (n-step Lead)

    Advance the lead pointer n steps first, then move both together at the same speed. When the lead reaches the end, the trailing pointer is exactly n positions from the end. Used for: Nth-from-End Node in a linked list.

    4. Fast / Slow — Tortoise and Hare

    Both move forward, slow by 1 step and fast by 2. If a cycle exists they will eventually meet; if the fast pointer reaches null, the list is acyclic. Also finds the middle of a list (stop when fast reaches the end).

    5. Palindrome / Symmetry

    Left at index 0, right at n−1; compare and move both inward. The first mismatch proves not a palindrome. O(n) time, O(1) space — no string copy needed. This is the pattern this visualization demonstrates.

    Choosing the right variant: Array sorted, need a pair? → Opposite-Ends. Filter or build in-place with no extra space? → Slow-Write / Fast-Read. Linked list cycle or midpoint? → Tortoise & Hare. Symmetry check on a string or array? → Palindrome pattern.

    When to reach for this

    • Finding a pair of elements that sum to X in a sorted array
    • Checking for Palindromes
    • In-place array manipulation (Remove Duplicates)
    • Container/Water problems

    Interview Questions

    Amazon
    Google
    Facebook
    Facebook
    Microsoft
    Amazon
    Google
    Amazon
    Goldman Sachs
    Bloomberg
    Uber
    LinkedIn
    Apple
    Airbnb
    Two Sigma

    Love this visualization?

    Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.

    100% free • No credit card required