Valid Palindrome Two Pointer in DSA Go - Time & Space Complexity
We want to know how long it takes to check if a string is a palindrome using two pointers.
How does the time grow as the string gets longer?
Analyze the time complexity of the following code snippet.
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
}
This code checks if a string reads the same forwards and backwards by comparing characters from both ends.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing characters at positions from the start and end moving towards the center.
- How many times: Up to half the length of the string (n/2 times).
As the string gets longer, the number of comparisons grows roughly half as fast.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5 comparisons |
| 100 | 50 comparisons |
| 1000 | 500 comparisons |
Pattern observation: The operations grow linearly with input size, but only half as many comparisons are needed.
Time Complexity: O(n)
This means the time to check grows directly with the length of the string.
[X] Wrong: "The loop runs n times, so it's O(n²) because of nested comparisons."
[OK] Correct: The loop runs only once through the string, comparing pairs, so it's just O(n), not squared.
Understanding this simple linear check helps build confidence for string problems and shows you can analyze how efficient your code is.
"What if we ignored non-alphanumeric characters and spaces? How would the time complexity change?"
Practice
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 AQuick Check:
Two-pointer = compare start/end chars [OK]
- Thinking it counts vowels instead of comparing characters
- Assuming it reverses the string explicitly
- Confusing sorting with palindrome check
s?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 DQuick Check:
Start=0, End=len(s)-1 [OK]
- Starting left at 1 instead of 0
- Setting right to len(s) which is out of range
- Using negative or invalid indices
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"))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 AQuick Check:
"racecar" palindrome = true, "hello" not = false [OK]
- Assuming "hello" is palindrome
- Confusing return values order
- Ignoring early return on mismatch
func isPalindrome(s string) bool {
left, right := 0, len(s)
for left < right {
if s[left] != s[right] {
return false
}
left++
right--
}
return true
}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 CQuick Check:
Index out of range error fixed by right = len(s)-1 [OK]
- Using len(s) as index causes runtime error
- Changing left pointer start incorrectly
- Misunderstanding loop condition
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?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 BQuick Check:
Clean and lowercase string before two-pointer check [OK]
- Not ignoring spaces and punctuation
- Comparing case-sensitively causing false negatives
- Only converting case without skipping symbols
Ready to prove it?
Pass the test and this pattern moves unknown
