Palindrome Detection in DSA Go - Time & Space Complexity
We want to know how the time needed to check if a word or phrase is a palindrome changes as the input gets longer.
How does the number of steps grow when the input size grows?
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 moving towards the center.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing characters at positions from the start and end of the string.
- How many times: Up to half the length of the string (n/2 times).
As the string gets longer, the number of character comparisons grows roughly half as fast.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5 comparisons |
| 100 | 50 comparisons |
| 1000 | 500 comparisons |
Pattern observation: The number of steps grows linearly with input size, but only half as many comparisons are needed.
Time Complexity: O(n)
This means the time to check a palindrome grows in direct proportion to the length of the string.
[X] Wrong: "Checking a palindrome takes constant time because you only compare a few characters."
[OK] Correct: The number of comparisons depends on the string length, so longer strings need more checks, not just a few.
Understanding how palindrome checking scales helps you explain your code clearly and shows you can analyze efficiency, a key skill in interviews.
"What if we changed the code to ignore spaces and punctuation? How would the time complexity change?"
Practice
Solution
Step 1: Understand palindrome definition
A palindrome is a word or phrase that reads the same from left to right and right to left.Step 2: Match options with definition
A string that reads the same forwards and backwards matches this definition exactly, others describe different string properties.Final Answer:
A string that reads the same forwards and backwards -> Option CQuick Check:
Palindrome = Same forwards and backwards [OK]
- Confusing palindrome with unique characters
- Thinking palindrome means only vowels
- Assuming palindrome ends with special character
s is a palindrome?Solution
Step 1: Analyze palindrome check logic
We compare characters from start and end moving towards center. If any pair differs, return false.Step 2: Match code with logic
for i := 0; i < len(s)/2; i++ { if s[i] != s[len(s)-1-i] { return false } } return true correctly compares s[i] with s[len(s)-1-i] for half the string length and returns false if mismatch found.Final Answer:
for i := 0; i < len(s)/2; i++ { if s[i] != s[len(s)-1-i] { return false } } return true -> Option AQuick Check:
Compare pairs from ends = for i := 0; i < len(s)/2; i++ { if s[i] != s[len(s)-1-i] { return false } } return true [OK]
- Checking only adjacent characters
- Returning false when characters match
- Comparing character with itself incorrectly
func isPalindrome(s string) bool {
for i := 0; i < len(s)/2; i++ {
if s[i] != s[len(s)-1-i] {
return false
}
}
return true
}
fmt.Println(isPalindrome("level"))
fmt.Println(isPalindrome("hello"))Solution
Step 1: Check "level" palindrome
"level" reads same forwards and backwards, so function returns true.Step 2: Check "hello" palindrome
"hello" reversed is "olleh", different, so function returns false.Final Answer:
true false -> Option DQuick Check:
"level"=true, "hello"=false [OK]
- Assuming all strings are palindrome
- Mixing output order
- Ignoring case sensitivity
func isPalindrome(s string) bool {
for i := 0; i <= len(s)/2; i++ {
if s[i] != s[len(s)-1-i] {
return false
}
}
return true
}Solution
Step 1: Analyze loop boundary
The loop uses 'i <= len(s)/2', which causes 'i' to reach len(s)/2 inclusive, possibly exceeding index for odd length strings.Step 2: Correct loop condition
Changing to 'i < len(s)/2' ensures no index out of range error and proper comparison only up to middle.Final Answer:
The loop condition should be 'i < len(s)/2' to avoid index out of range -> Option AQuick Check:
Loop boundary off by one causes error [OK]
- Using <= instead of < in loop condition
- Returning wrong boolean at end
- Starting loop from 1 instead of 0
Solution
Step 1: Understand problem requirements
We must ignore spaces and case differences when checking palindrome.Step 2: Prepare string correctly
Removing spaces and converting all letters to lowercase normalizes the string for accurate palindrome check.Step 3: Check palindrome
After normalization, use standard palindrome check comparing characters from ends.Final Answer:
Remove spaces, convert to lowercase, then check palindrome normally -> Option BQuick Check:
Normalize string before check = Remove spaces, convert to lowercase, then check palindrome normally [OK]
- Ignoring spaces in palindrome check
- Not converting to lowercase
- Comparing raw strings directly
Ready to prove it?
Pass the test and this pattern moves unknown
