Check if a String is a Palindrome Using Two Pointers
📖 Scenario: Imagine you want to check if a word or sentence reads the same forwards and backwards, ignoring spaces, punctuation, and letter case. This is called a palindrome check. For example, "Racecar" and "A man, a plan, a canal: Panama" are palindromes.
🎯 Goal: You will build a program that uses two pointers to check if a given string is a palindrome. The program will ignore spaces, punctuation, and case differences.
📋 What You'll Learn
Create a string variable with a specific sentence.
Create two integer variables to act as pointers.
Use a loop to move the pointers towards each other and compare characters.
Print true if the string is a palindrome, otherwise false.
💡 Why This Matters
🌍 Real World
Palindrome checks are useful in text processing, data validation, and coding challenges where symmetry matters.
💼 Career
Understanding two-pointer techniques helps in solving string and array problems efficiently, a common task in software development interviews.
Progress0 / 4 steps
1
Create the input string
Create a string variable called input and set it to the exact value "A man, a plan, a canal: Panama".
DSA Go
Hint
Use input := "A man, a plan, a canal: Panama" to create the string.
2
Initialize two pointers
Add two integer variables called left and right. Set left to 0 and right to the last index of input (use len(input) - 1).
DSA Go
Hint
Use left := 0 and right := len(input) - 1 to set the pointers.
3
Use two pointers to check palindrome
Write a for loop that runs while left < right. Inside the loop, skip non-letter and non-digit characters by moving left forward and right backward. Compare characters at left and right ignoring case. If they differ, print false and return. If they match, move left forward and right backward.
DSA Go
Hint
Use nested for loops to skip non-alphanumeric characters and compare characters ignoring case.
4
Print the final result
After the loop, print true to show the string is a palindrome.
DSA Go
Hint
Use fmt.Println(true) after the loop to print the result.
Practice
(1/5)
1. What does the two-pointer technique check when verifying if a string is a palindrome?
easy
A. It compares characters from the start and end moving towards the center
B. It counts the number of vowels in the string
C. It reverses the string and compares with the original
D. It sorts the characters and checks if sorted equals original
Solution
Step 1: Understand two-pointer approach
The two-pointer technique uses two indexes: one at the start and one at the end of the string.
Step 2: Compare characters moving inward
It compares characters at these pointers and moves them towards the center to check if they match.
Final Answer:
It compares characters from the start and end moving towards the center -> Option A
Quick Check:
Two-pointer = compare start/end chars [OK]
Hint: Check characters from both ends moving inward [OK]
Common Mistakes:
Thinking it counts vowels instead of comparing characters
Assuming it reverses the string explicitly
Confusing sorting with palindrome check
2. Which of the following Go code snippets correctly initializes two pointers for a palindrome check on string s?
easy
A. left := len(s); right := 0
B. left := 1; right := len(s)
C. left := -1; right := len(s) + 1
D. left := 0; right := len(s) - 1
Solution
Step 1: Identify valid pointer initialization
For a string, the first character index is 0 and last is length minus 1.
Step 2: Check each option
left := 0; right := len(s) - 1 correctly sets left to 0 and right to len(s)-1, valid for two-pointer palindrome check.
Final Answer:
left := 0; right := len(s) - 1 -> Option D
Quick Check:
Start=0, End=len(s)-1 [OK]
Hint: Start left at 0 and right at len(s)-1 [OK]
Common Mistakes:
Starting left at 1 instead of 0
Setting right to len(s) which is out of range
Using negative or invalid indices
3. What will be the output of the following Go code?
func isPalindrome(s string) bool {
left, right := 0, len(s)-1
for left < right {
if s[left] != s[right] {
return false
}
left++
right--
}
return true
}
fmt.Println(isPalindrome("racecar"))
fmt.Println(isPalindrome("hello"))
medium
A. true\nfalse
B. false\ntrue
C. true\ntrue
D. false\nfalse
Solution
Step 1: Trace "racecar" input
Characters at start and end match moving inward, so function returns true.
Step 2: Trace "hello" input
Characters 'h' and 'o' differ at first check, so function returns false.
Final Answer:
true\nfalse -> Option A
Quick Check:
"racecar" palindrome = true, "hello" not = false [OK]
Hint: Check first and last chars; mismatch means false [OK]
Common Mistakes:
Assuming "hello" is palindrome
Confusing return values order
Ignoring early return on mismatch
4. Identify the bug in this Go function that checks palindrome:
func isPalindrome(s string) bool {
left, right := 0, len(s)
for left < right {
if s[left] != s[right] {
return false
}
left++
right--
}
return true
}
medium
A. left should start at 1, not 0
B. The loop condition should be left <= right
C. right should be initialized to len(s)-1, not len(s)
D. The function should return false at the end
Solution
Step 1: Check pointer initialization
right is set to len(s), which is out of string index range (0 to len(s)-1).
Step 2: Correct initialization
right should be len(s)-1 to point to last character correctly.
Final Answer:
right should be initialized to len(s)-1, not len(s) -> Option C
Quick Check:
Index out of range error fixed by right = len(s)-1 [OK]
Hint: Set right pointer to len(s)-1 to avoid index error [OK]
Common Mistakes:
Using len(s) as index causes runtime error
Changing left pointer start incorrectly
Misunderstanding loop condition
5. Given the string s = "A man, a plan, a canal: Panama", which modification to the two-pointer palindrome check is needed to correctly identify it as a palindrome?
hard
A. Reverse the string and compare directly
B. Ignore non-alphanumeric characters and compare case-insensitively
C. Only compare vowels in the string
D. Convert string to uppercase and compare characters directly
Solution
Step 1: Understand input complexity
The string contains spaces, commas, and mixed cases which should be ignored for palindrome check.
Step 2: Modify two-pointer logic
Skip non-alphanumeric characters and compare characters in lowercase to handle case differences.
Final Answer:
Ignore non-alphanumeric characters and compare case-insensitively -> Option B
Quick Check:
Clean and lowercase string before two-pointer check [OK]
Hint: Skip symbols and compare lowercase chars [OK]