Bird
0
0
DSA Cprogramming~10 mins

Palindrome Detection in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Palindrome Detection
Start
Set left = 0, right = length-1
Compare s[left
Return False
Yes
Increment left, Decrement right
Check if left >= right
Return True
No
left
Start with two pointers at the string ends, compare characters moving inward until mismatch or pointers cross.
Execution Sample
DSA C
int isPalindrome(char *s) {
  int left = 0, right = strlen(s) - 1;
  while (left < right) {
    if (s[left] != s[right]) return 0;
    left++; right--;
  }
  return 1;
}
Checks if a string is a palindrome by comparing characters from both ends moving inward.
Execution Table
StepOperationleftrightCharacters ComparedMatch?ActionVisual State
1Initialize pointers06--Set left=0, right=6"r" "a" "c" "e" "c" "a" "r"
2Compare s[left] and s[right]06r vs rYesleft++, right--"r" "a" "c" "e" "c" "a" "r"
3Compare s[left] and s[right]15a vs aYesleft++, right--"r" "a" "c" "e" "c" "a" "r"
4Compare s[left] and s[right]24c vs cYesleft++, right--"r" "a" "c" "e" "c" "a" "r"
5Compare s[left] and s[right]33e vs eYesleft >= right, return True"r" "a" "c" "e" "c" "a" "r"
6End----Palindrome detected"r" "a" "c" "e" "c" "a" "r"
💡 Pointers crossed at step 5, all characters matched, string is palindrome
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
left012333
right654333
Key Moments - 3 Insights
Why do we stop comparing when left >= right?
Because when left meets or passes right, all pairs have been checked. See execution_table step 5 where left=3 and right=3 means middle reached.
What if characters at left and right don't match?
We immediately return false as shown in execution_table step 2-4 'Match?' column. If any mismatch occurs, palindrome check fails.
Why do we increment left and decrement right after each successful comparison?
To move inward and compare the next pair of characters, as shown in execution_table steps 2-4 'Action' column.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what are the values of left and right?
Aleft=2, right=4
Bleft=3, right=3
Cleft=1, right=5
Dleft=0, right=6
💡 Hint
Check the 'left' and 'right' columns in execution_table row with Step=3
At which step does the function decide the string is a palindrome?
AStep 5
BStep 2
CStep 4
DStep 6
💡 Hint
Look at the 'Action' column where it returns True when left >= right
If the string was "racecarx", what would happen at step 2?
ACharacters match, continue
BCharacters don't match, return false
CPointers cross, return true
DError due to string length
💡 Hint
Refer to 'Match?' column in execution_table for mismatch behavior
Concept Snapshot
Palindrome Detection:
- Use two pointers: left at start, right at end
- Compare characters s[left] and s[right]
- If mismatch, return false immediately
- If pointers cross or meet, return true
- Moves inward by left++ and right-- each step
Full Transcript
Palindrome detection uses two pointers starting at the ends of the string. We compare characters at these pointers. If they match, we move the pointers inward. If any pair does not match, the string is not a palindrome. When the pointers meet or cross, it means all pairs matched and the string is a palindrome.