Bird
Raised Fist0
DSA Cprogramming~10 mins

Valid Palindrome Two Pointer in DSA C - Execution Trace

Choose your learning style10 modes available
Concept Flow - Valid Palindrome Two Pointer
Start: left=0, right=length-1
↓
Check if left < right?
No→Done: Palindrome
Yes↓
Is s[left
Yes↓
Is s[right
Yes↓
Compare s[left
↓
left++, right--
Two pointers start at the string ends, move inward skipping non-alphanumeric characters, comparing characters case-insensitively until pointers cross or mismatch.
Execution Sample
DSA C
bool isPalindrome(char * s) {
    int left = 0, right = strlen(s) - 1;
    while (left < right) {
        if (!isalnum(s[left])) left++;
        else if (!isalnum(s[right])) right--;
        else if (tolower(s[left]) != tolower(s[right])) return false;
        else { left++; right--; }
    }
    return true;
}
Checks if a string is a palindrome ignoring non-alphanumeric characters and case using two pointers.
Execution Table
StepOperationleftrightCharacters ComparedActionVisual State
1Initialize pointers029-Start"A man, a plan, a canal: Panama"
2Check s[left] alnum029A vs aCompare (equal)"A man, a plan, a canal: Panama"
3Move pointers128-left++, right--"A man, a plan, a canal: Panama"
4Check s[left] alnum128 vs mleft not alnum, left++"A man, a plan, a canal: Panama"
5Check s[left] alnum228m vs mCompare (equal)"A man, a plan, a canal: Panama"
6Move pointers327-left++, right--"A man, a plan, a canal: Panama"
7Check s[left] alnum327a vs aCompare (equal)"A man, a plan, a canal: Panama"
8Move pointers426-left++, right--"A man, a plan, a canal: Panama"
9Check s[left] alnum426n vs nCompare (equal)"A man, a plan, a canal: Panama"
10Move pointers525-left++, right--"A man, a plan, a canal: Panama"
11Check s[left] alnum525, vs aleft not alnum, left++"A man, a plan, a canal: Panama"
12Check s[left] alnum625 vs aleft not alnum, left++"A man, a plan, a canal: Panama"
13Check s[left] alnum725a vs aCompare (equal)"A man, a plan, a canal: Panama"
14Move pointers824-left++, right--"A man, a plan, a canal: Panama"
15Check s[left] alnum824 vs Pleft not alnum, left++"A man, a plan, a canal: Panama"
16Check s[left] alnum924p vs PCompare (equal)"A man, a plan, a canal: Panama"
17Move pointers1023-left++, right--"A man, a plan, a canal: Panama"
18Check s[left] alnum1023l vs right not alnum, right--"A man, a plan, a canal: Panama"
19Check s[left] alnum1022l vs :right not alnum, right--"A man, a plan, a canal: Panama"
20Check s[left] alnum1021l vs lCompare (equal)"A man, a plan, a canal: Panama"
21Move pointers1120-left++, right--"A man, a plan, a canal: Panama"
22Check s[left] alnum1120a vs aCompare (equal)"A man, a plan, a canal: Panama"
23Move pointers1219-left++, right--"A man, a plan, a canal: Panama"
24Check s[left] alnum1219n vs nCompare (equal)"A man, a plan, a canal: Panama"
25Move pointers1318-left++, right--"A man, a plan, a canal: Panama"
26Check s[left] alnum1318, vs aleft not alnum, left++"A man, a plan, a canal: Panama"
💡 After step 26 (left=14, right=18), continue similarly: skip left ' ' at 14 to 15, compare 'a' vs 'a' at 15/18, move to 16/17; skip left ' ' at 16 to 17; left == right =17, all pairs matched, palindrome.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8After 9After 10Final
left012345789101017
right292828272625252424232117
Key Moments - 3 Insights
Why do we skip characters that are not alphanumeric?
Because the palindrome check ignores spaces, punctuation, and symbols. See steps 4, 11, 12, 15, and 26 in the execution_table where pointers move without comparison.
Why do we compare characters in lowercase?
To make the comparison case-insensitive. For example, 'A' and 'a' are considered equal as shown in step 2 and 'p' and 'P' in step 16.
When does the loop stop?
The loop stops when left pointer is no longer less than right pointer, meaning all pairs have been checked. This occurs when pointers meet or cross after final skips (left=17, right=17).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'left' after step 5?
A2
B3
C1
D5
💡 Hint
Check the 'left' column in execution_table row with Step 5.
At which step does the right pointer first move without comparison because of a non-alphanumeric character?
AStep 4
BStep 11
CStep 18
DStep 25
💡 Hint
Look for steps where 'Action' says 'right not alnum, right--' in execution_table.
If the input string was "race a car", at which step would the function return false?
AWhen comparing 'e' and 'a'
BWhen comparing 'r' and 'r'
CWhen comparing 'c' and 'c'
DWhen comparing 'a' and 'a'
💡 Hint
Think about which characters mismatch in the palindrome check and when the function returns false.
Concept Snapshot
Two pointers start at string ends
Skip non-alphanumeric chars
Compare characters case-insensitive
Move pointers inward if equal
Stop when pointers cross or mismatch
Full Transcript
This visualization shows how to check if a string is a palindrome using two pointers. We start with one pointer at the beginning and one at the end. We skip characters that are not letters or numbers. We compare characters ignoring case. If they match, we move both pointers inward. If they don't match, we stop and say it's not a palindrome. The process continues until the pointers cross, meaning the string is a palindrome.

Practice

(1/5)
1. What is the main idea behind the two-pointer technique to check if a string is a palindrome?
easy
A. Count the frequency of each character and check if all are even
B. Sort the string and check if it matches the original
C. Reverse the string and compare with the original
D. Use two pointers starting from both ends and move towards the center comparing characters

Solution

  1. Step 1: Understand two-pointer setup

    Two pointers start at the beginning and end of the string to compare characters.
  2. Step 2: Move pointers inward while characters match

    If characters at both pointers are equal, move pointers closer until they meet or cross.
  3. Final Answer:

    Use two pointers starting from both ends and move towards the center comparing characters -> Option D
  4. Quick Check:

    Two-pointer palindrome check = Use two pointers starting from both ends and move towards the center comparing characters [OK]
Hint: Compare characters from ends moving inward [OK]
Common Mistakes:
  • Trying to sort the string which changes order
  • Only checking half the string without pointers
  • Ignoring non-alphanumeric characters if required
2. Which of the following C code snippets correctly initializes two pointers for palindrome checking on a string s?
easy
A. int left = 0, right = strlen(s) - 1;
B. int left = 1, right = strlen(s);
C. int left = 0, right = strlen(s);
D. int left = 1, right = strlen(s) - 1;

Solution

  1. Step 1: Understand string indexing in C

    Strings are zero-indexed, so first character is at 0 and last at length-1.
  2. Step 2: Correct pointer initialization

    Left pointer starts at 0, right pointer at strlen(s)-1 to cover full string.
  3. Final Answer:

    int left = 0, right = strlen(s) - 1; -> Option A
  4. Quick Check:

    Indexing starts at 0, last index = length-1 [OK]
Hint: Start left at 0 and right at length minus one [OK]
Common Mistakes:
  • Setting right pointer to strlen(s) which is out of bounds
  • Starting left pointer at 1 skipping first character
  • Using wrong indices causing runtime errors
3. What will be the output of the following C code snippet?
char s[] = "racecar";
int left = 0, right = strlen(s) - 1;
int isPalindrome = 1;
while (left < right) {
    if (s[left] != s[right]) {
        isPalindrome = 0;
        break;
    }
    left++;
    right--;
}
printf("%d", isPalindrome);
medium
A. 0
B. 1
C. 7
D. Compilation error

Solution

  1. Step 1: Trace pointer comparisons on "racecar"

    Characters at left and right match: 'r'=='r', 'a'=='a', 'c'=='c', 'e'=='e'.
  2. Step 2: Loop completes without mismatch

    No mismatch found, so isPalindrome remains 1 and prints 1.
  3. Final Answer:

    1 -> Option B
  4. Quick Check:

    "racecar" is palindrome = 1 [OK]
Hint: Check matching pairs from ends; no mismatch means palindrome [OK]
Common Mistakes:
  • Assuming output is string instead of integer
  • Confusing index bounds causing wrong comparisons
  • Forgetting to break loop on mismatch
4. Identify the bug in the following C code that checks if a string s is a palindrome using two pointers:
int left = 0, right = strlen(s);
while (left <= right) {
    if (s[left] != s[right]) {
        return 0;
    }
    left++;
    right--;
}
return 1;
medium
A. Both A and B are bugs
B. Loop condition should be left < right, not left <= right
C. Right pointer should be initialized to strlen(s) - 1
D. No bug, code is correct

Solution

  1. Step 1: Check right pointer initialization

    Right pointer set to strlen(s) is out of bounds; should be strlen(s) - 1.
  2. Step 2: Check loop condition

    Loop should run while left < right to avoid comparing same middle character twice.
  3. Final Answer:

    Both A and B are bugs -> Option A
  4. Quick Check:

    Right pointer and loop condition both incorrect = Both A and B are bugs [OK]
Hint: Right pointer = length-1 and loop: left < right [OK]
Common Mistakes:
  • Using strlen(s) as right pointer causing out-of-bounds access
  • Using <= in loop causing unnecessary comparison
  • Ignoring off-by-one errors
5. Given a string s that may contain uppercase letters, lowercase letters, and non-alphanumeric characters, which approach correctly checks if s is a palindrome ignoring cases and non-alphanumeric characters using two pointers?
hard
A. Count frequency of alphanumeric characters and check if all counts are even
B. Convert entire string to lowercase, remove non-alphanumeric chars, then compare with reversed string
C. Use two pointers from ends, skip non-alphanumeric chars, compare lowercase characters
D. Use two pointers but compare characters directly without skipping or case conversion

Solution

  1. Step 1: Use two pointers and skip non-alphanumeric characters

    Move left and right pointers inward, skipping characters that are not letters or digits.
  2. Step 2: Compare characters in lowercase form

    Convert characters to lowercase before comparing to ignore case differences.
  3. Final Answer:

    Use two pointers from ends, skip non-alphanumeric chars, compare lowercase characters -> Option C
  4. Quick Check:

    Two-pointer with skip and lowercase = Use two pointers from ends, skip non-alphanumeric chars, compare lowercase characters [OK]
Hint: Skip non-alphanumeric and compare lowercase chars with two pointers [OK]
Common Mistakes:
  • Comparing characters without skipping non-alphanumeric
  • Not converting characters to lowercase before comparison
  • Using frequency count which doesn't check order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard