Palindrome Detection in DSA C - 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.
The question is: how does the number of steps grow when the input size grows?
Analyze the time complexity of the following code snippet.
#include <stdio.h>
#include <string.h>
int isPalindrome(char str[]) {
int left = 0;
int right = strlen(str) - 1;
while (left < right) {
if (str[left] != str[right]) {
return 0;
}
left++;
right--;
}
return 1;
}
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 input string.
[X] Wrong: "The palindrome check takes constant time because it just compares characters."
[OK] Correct: The number of comparisons depends on the string length, so longer strings take more time.
Understanding how palindrome detection scales helps you explain your code clearly and shows you can analyze efficiency, a key skill in interviews.
"What if we checked only every other character instead of all? How would the time complexity change?"
Practice
Solution
Step 1: Understand palindrome definition
A palindrome is a word or phrase that reads the same forwards and backwards, ignoring spaces and punctuation.Step 2: Compare options with definition
Only A string that reads the same forwards and backwards matches this definition exactly.Final Answer:
A string that reads the same forwards and backwards -> Option DQuick Check:
Palindrome = same forwards and backwards [OK]
- Confusing palindrome with unique characters
- Thinking palindrome means only vowels
- Assuming palindrome ends with special characters
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, string is not palindrome.Step 2: Match code with logic
for (int i = 0; i < strlen(s)/2; i++) { if (s[i] != s[strlen(s)-1-i]) return 0; } return 1; correctly compares s[i] and s[length-1-i] for half the string length and returns 0 if mismatch found, else 1.Final Answer:
for (int i = 0; i < strlen(s)/2; i++) { if (s[i] != s[strlen(s)-1-i]) return 0; } return 1; -> Option AQuick Check:
Compare front and back chars for half length [OK]
- Comparing adjacent characters instead of opposite ends
- Returning 1 too early inside loop
- Incorrect loop range causing out-of-bounds
char s[] = "radar";
int i, flag = 1;
int n = strlen(s);
for (i = 0; i < n/2; i++) {
if (s[i] != s[n - i - 1]) {
flag = 0;
break;
}
}
printf("%d", flag);Solution
Step 1: Understand the code logic
The code checks if string "radar" is palindrome by comparing characters from start and end.Step 2: Trace the loop comparisons
Characters match at all pairs: r-r, a-a, d-d (middle ignored). So flag remains 1.Final Answer:
1 -> Option BQuick Check:
"radar" is palindrome, flag = 1 [OK]
- Assuming flag is 0 for palindrome
- Confusing loop range causing missed checks
- Misreading string length calculation
int isPalindrome(char *s) {
int n = strlen(s);
for (int i = 0; i <= n/2; i++) {
if (s[i] != s[n - i]) {
return 0;
}
}
return 1;
}Solution
Step 1: Check array indexing in condition
String indices go from 0 to n-1, so s[n - i] accesses out-of-bounds when i=0.Step 2: Correct index to n - i - 1
Changing to s[n - i - 1] fixes indexing and prevents runtime error.Final Answer:
Index n - i should be n - i - 1 to avoid out-of-bounds -> Option CQuick Check:
Array index must be within 0 to n-1 [OK]
- Using wrong loop boundary causing extra iteration
- Ignoring off-by-one in array indexing
- Assuming function return type is incorrect
char s[] = "A man a plan a canal Panama";
Solution
Step 1: Normalize string by removing spaces and converting to lowercase
Spaces and case differences affect palindrome check, so remove spaces and convert all letters to lowercase.Step 2: Perform palindrome check on normalized string
After normalization, check if string reads same forwards and backwards.Final Answer:
Remove spaces and convert to lowercase, then check palindrome normally -> Option AQuick Check:
Normalize string before palindrome check [OK]
- Ignoring spaces causing false negatives
- Ignoring case differences causing false negatives
- Checking palindrome on raw string directly
Ready to prove it?
Pass the test and this pattern moves unknown
