Skip to content

Valid Palindrome With Two Pointers
Step by Step

Two pointers compare the ends of a string and walk inward. One mismatch settles the answer.

Step through every comparison in both directions, then share a link to the step where the pointers crossed.

What You Watch While Pointers Walk Inward

Both indexes and the ruled out span change on every step.

Valid palindrome checks the two ends first. Matching ends move both inward, and one mismatch ends the check.

CodeStory's visualiser shows both indexes walking and the span they ruled out. Marked code lines follow every move, and you can step back, step forward or play.

Open Odd length first. Then load Fails at the ends to see one check settle the answer.

How Valid Palindrome Checks Both Ends

Steps below follow the two indexes on your screen.

  1. 1Place one index at the start and one at the end.
  2. 2Compare the two characters.
  3. 3Stop on a mismatch, or move both indexes inward on a match.
  4. 4Stop when the indexes meet or cross.

Valid Palindrome Inputs Worth Trying

Three presets cover odd length, even length and an early fail.

  • Odd lengthWatch the middle character, which never gets compared.
  • Even lengthNo middle character exists, so the pointers cross.
  • Fails at the endsThe inside is a palindrome, but the first check already says no.

Valid Palindrome Cost in Time and Space

Two pointers cross the string once, so the check stays linear.

Time
O(n)counted from the comparisons you step through
Space
O(1)only two index values besides the input
Pattern
Two pointersTwo indexes walk inward and rule out characters.

Practise Two Pointers Beyond Palindromes

Watching shows the pointers and solving trains you to choose which one moves.

Everything in the App in One Place

Practice, interviews and tools all start free. Swipe to see the rest.

Sign up for 100 AI credits now and get 100 more every month. Start free in the app

Valid Palindrome Visualiser FAQs

Open the visualiser and follow both pointers inward.

Open the visualiser
What does the valid palindrome visualiser show?

CodeStory's visualiser shows both indexes walking inward and the span they ruled out, beside the highlighted code. Move back and forth through every step, or let it play.

What is the time complexity of valid palindrome?

O(n) time and O(1) space. Live counts show the comparisons growing with string length.

How do you check a palindrome with two pointers?

Place one pointer at both ends and compare the characters. Move both inward on a match and stop on the first mismatch.

Can I use my own input?

Yes. Enter your own input and the visualiser plays the algorithm on it, including the edge cases that break a solution in an interview.

Is the visualiser free?

Yes. Every new account starts with 100 AI credits and gets 100 more monthly, while watching an algorithm costs nothing.

Stuck on Two Pointers? Get a Free Callback

Tell CodeStory which two pointers step loses you, and a mentor plans what comes next.

  1. 1CodeStory checks your level, from two pointers basics to interview ready.
  2. 2CodeStory explains online and campus batches, with timings for your weekdays or weekends.
  3. 3You decide. No pressure, no payment on the call.

Get a free callback

I am a
I want to learn

Your number is only used for a CodeStory callback.