Check if a String is a Palindrome
Table of Contents + β
This one looks simple. The interviewer asks you to check if a word reads the same forwards and backwards. But what they really want to see is whether you reach for extra memory by reflex, or whether you can solve it in place with two pointers. So let us talk it through the way you would in the room.
π The Problem
You are given a string. You have to tell if it is a palindrome. A palindrome is a string that reads the same from the front and from the back. So βmadamβ read backwards is still βmadamβ. But βhelloβ backwards is βollehβ, which is different.
Here is what the input and output look like:
Input: "madam"Output: true
Input: "hello"Output: falseπ’ The Simple Approach (and why it is not the best)
The first idea most people get is easy. Just reverse the string. Then check if the reversed string equals the original one. If they match, it is a palindrome.
That works, and it is correct. But see the problem. To reverse the string, you build a whole new string in memory. So for a string of length n, you spend extra O(n) space just to hold the copy. The interviewer will ask, βcan you do it without making a copy?β. And that is the real test here.
π― The Better Approach: Two Pointers
Here is the trick. You do not need a copy at all. You put one pointer at the start of the string. You put another pointer at the end. Then you compare those two characters.
If they are the same, you move the left pointer one step right. And you move the right pointer one step left. Then you compare again. You keep going until the two pointers meet in the middle.
If at any point the two characters do not match, you stop right away and return false. If you reach the middle without finding a mismatch, then it is a palindrome. This is the two-pointer technique. You never build a second string, so you use only O(1) extra space.
Think of it like two people checking a row of seats. One starts from the left end. The other starts from the right end. They walk toward each other and check that each pair of seats matches. The moment a pair does not match, they know something is wrong and they stop.
Steps to Check a Palindrome
- Put a pointer
leftat index 0 and a pointerrightat the last index. - While
leftis less thanright, compare the characters at the two pointers. - If the characters are different, return false right away.
- If they are the same, move
leftone step right andrightone step left. - When the loop ends without a mismatch, return true.
# Returns True if the string reads the same both waysdef is_palindrome(s): left = 0 right = len(s) - 1 while left < right: if s[left] != s[right]: # mismatch found return False left += 1 # move left pointer inward right -= 1 # move right pointer inward return True # reached the middle with no mismatch
print(is_palindrome("madam"))print(is_palindrome("hello"))The output of the above code will be:
TrueFalseβ±οΈ Time and Space Complexity
Both approaches look at every character once, so the time is the same. The big difference is the memory. The two-pointer way wins because it never makes a copy.
| Approach | Time | Space |
|---|---|---|
| Reverse and compare | O(n) | O(n) |
| Two pointers | O(n) | O(1) |
Tip
Many interviewers add a twist. They ask you to ignore case and skip non-letter characters, so βA man, a plan, a canal: Panamaβ counts as a palindrome. The fix is small. When a pointer lands on a non-letter, just skip it and move on. And compare the characters in lowercase. The two-pointer shape stays exactly the same.
π§© Key Takeaways
- β A palindrome reads the same from the front and from the back.
- β Reversing and comparing works but costs O(n) extra space for the copy.
- β The two-pointer method checks from both ends inward in O(n) time and O(1) space.
- β Stop and return false the moment two characters do not match.
- β For the case-insensitive version, skip non-letters and compare in lowercase.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What makes a string a palindrome?
Why: A palindrome is exactly a string that reads the same from the front and from the back, like 'madam'.
- 2
Why is the two-pointer approach better than reversing the string?
Why: Reversing builds a full copy that needs O(n) space, while two pointers compare in place using only O(1) extra space.
- 3
When should the two-pointer loop stop?
Why: The pointers move inward and the loop ends once they meet in the middle, since every pair has then been checked.
- 4
What should the function do the moment two compared characters differ?
Why: A single mismatch already proves the string is not a palindrome, so you can return false immediately.
π Whatβs Next?
- Reverse a String β uses the same two-pointer idea.