Bird
Raised Fist0
DSA Cprogramming~20 mins

Palindrome Detection in DSA C - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Palindrome Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of palindrome check for a simple string
What is the output of this C code that checks if a string is a palindrome?
DSA C
#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;
}

int main() {
    char s[] = "radar";
    if (isPalindrome(s)) {
        printf("Palindrome\n");
    } else {
        printf("Not Palindrome\n");
    }
    return 0;
}
APalindrome
BNot Palindrome
CCompilation Error
DRuntime Error
Attempts:
2 left
💡 Hint
Check if the string reads the same forwards and backwards.
❓ Predict Output
intermediate
2:00remaining
Output for palindrome check with mixed characters
What will this C program print when checking the string "hello"?
DSA C
#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;
}

int main() {
    char s[] = "hello";
    if (isPalindrome(s)) {
        printf("Palindrome\n");
    } else {
        printf("Not Palindrome\n");
    }
    return 0;
}
APalindrome
BRuntime Error
CCompilation Error
DNot Palindrome
Attempts:
2 left
💡 Hint
Check if the first and last characters are the same.
🔧 Debug
advanced
2:00remaining
Identify the bug in palindrome detection code
What error does this code produce when checking the string "abba"?
DSA C
#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;
}

int main() {
    char s[] = "abba";
    if (isPalindrome(s)) {
        printf("Palindrome\n");
    } else {
        printf("Not Palindrome\n");
    }
    return 0;
}
ACompilation Error
BPrints "Palindrome"
CRuntime Error (out-of-bounds access)
DPrints "Not Palindrome"
Attempts:
2 left
💡 Hint
Check how the right index is initialized.
🧠 Conceptual
advanced
1:00remaining
Time complexity of palindrome detection
What is the time complexity of checking if a string of length n is a palindrome using the two-pointer approach?
AO(n)
BO(n^2)
CO(log n)
DO(1)
Attempts:
2 left
💡 Hint
Consider how many character comparisons are made.
❓ Predict Output
expert
3:00remaining
Output of palindrome check ignoring case and non-alphanumeric characters
What is the output of this C program that checks if a string is a palindrome ignoring case and non-alphanumeric characters?
DSA C
#include <stdio.h>
#include <ctype.h>
#include <string.h>

int isAlphaNum(char c) {
    return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9');
}

int isPalindrome(char str[]) {
    int left = 0;
    int right = strlen(str) - 1;
    while (left < right) {
        while (left < right && !isAlphaNum(str[left])) left++;
        while (left < right && !isAlphaNum(str[right])) right--;
        if (tolower(str[left]) != tolower(str[right])) {
            return 0;
        }
        left++;
        right--;
    }
    return 1;
}

int main() {
    char s[] = "A man, a plan, a canal: Panama";
    if (isPalindrome(s)) {
        printf("Palindrome\n");
    } else {
        printf("Not Palindrome\n");
    }
    return 0;
}
ANot Palindrome
BPalindrome
CCompilation Error
DRuntime Error
Attempts:
2 left
💡 Hint
Ignore spaces, commas, and case differences.

Practice

(1/5)
1. What is a palindrome in the context of strings?
easy
A. A string that contains only vowels
B. A string that ends with a special character
C. A string with all unique characters
D. A string that reads the same forwards and backwards

Solution

  1. Step 1: Understand palindrome definition

    A palindrome is a word or phrase that reads the same forwards and backwards, ignoring spaces and punctuation.
  2. Step 2: Compare options with definition

    Only A string that reads the same forwards and backwards matches this definition exactly.
  3. Final Answer:

    A string that reads the same forwards and backwards -> Option D
  4. Quick Check:

    Palindrome = same forwards and backwards [OK]
Hint: Check if string equals its reverse [OK]
Common Mistakes:
  • Confusing palindrome with unique characters
  • Thinking palindrome means only vowels
  • Assuming palindrome ends with special characters
2. Which of the following C code snippets correctly checks if a string s is a palindrome?
easy
A. for (int i = 0; i < strlen(s)/2; i++) { if (s[i] != s[strlen(s)-1-i]) return 0; } return 1;
B. for (int i = 0; i < strlen(s); i++) { if (s[i] == s[i+1]) return 1; } return 0;
C. for (int i = 0; i < strlen(s); i++) { if (s[i] != s[i+1]) return 0; } return 1;
D. for (int i = 0; i < strlen(s)/2; i++) { if (s[i] == s[strlen(s)-1-i]) return 0; } return 1;

Solution

  1. Step 1: Analyze palindrome check logic

    We compare characters from start and end moving towards center. If any pair differs, string is not palindrome.
  2. 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.
  3. Final Answer:

    for (int i = 0; i < strlen(s)/2; i++) { if (s[i] != s[strlen(s)-1-i]) return 0; } return 1; -> Option A
  4. Quick Check:

    Compare front and back chars for half length [OK]
Hint: Check characters from ends moving inward [OK]
Common Mistakes:
  • Comparing adjacent characters instead of opposite ends
  • Returning 1 too early inside loop
  • Incorrect loop range causing out-of-bounds
3. What will be the output of the following C code?
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);
medium
A. 0
B. 1
C. Compilation error
D. Undefined behavior

Solution

  1. Step 1: Understand the code logic

    The code checks if string "radar" is palindrome by comparing characters from start and end.
  2. Step 2: Trace the loop comparisons

    Characters match at all pairs: r-r, a-a, d-d (middle ignored). So flag remains 1.
  3. Final Answer:

    1 -> Option B
  4. Quick Check:

    "radar" is palindrome, flag = 1 [OK]
Hint: Check if string equals reverse, print 1 if yes [OK]
Common Mistakes:
  • Assuming flag is 0 for palindrome
  • Confusing loop range causing missed checks
  • Misreading string length calculation
4. Identify the error in this palindrome check code snippet:
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;
}
medium
A. Loop condition should be i < n/2, not i <= n/2
B. Function should return void instead of int
C. Index n - i should be n - i - 1 to avoid out-of-bounds
D. No error, code works correctly

Solution

  1. 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.
  2. Step 2: Correct index to n - i - 1

    Changing to s[n - i - 1] fixes indexing and prevents runtime error.
  3. Final Answer:

    Index n - i should be n - i - 1 to avoid out-of-bounds -> Option C
  4. Quick Check:

    Array index must be within 0 to n-1 [OK]
Hint: Remember last index is length - 1 [OK]
Common Mistakes:
  • Using wrong loop boundary causing extra iteration
  • Ignoring off-by-one in array indexing
  • Assuming function return type is incorrect
5. Given a string with mixed case and spaces, which approach correctly checks if it is a palindrome ignoring case and spaces?
char s[] = "A man a plan a canal Panama";
hard
A. Remove spaces and convert to lowercase, then check palindrome normally
B. Check palindrome directly without modifications
C. Only convert to lowercase, ignore spaces
D. Only remove spaces, ignore case

Solution

  1. 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.
  2. Step 2: Perform palindrome check on normalized string

    After normalization, check if string reads same forwards and backwards.
  3. Final Answer:

    Remove spaces and convert to lowercase, then check palindrome normally -> Option A
  4. Quick Check:

    Normalize string before palindrome check [OK]
Hint: Clean string first: lowercase + no spaces [OK]
Common Mistakes:
  • Ignoring spaces causing false negatives
  • Ignoring case differences causing false negatives
  • Checking palindrome on raw string directly
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