Bird
0
0
DSA Cprogramming~20 mins

Longest Palindromic Substring in DSA C - Practice Problems & Challenges

Choose your learning style9 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 longest palindromic substring length
What is the output of this C code that finds the length of the longest palindromic substring in the string "babad"?
DSA C
#include <stdio.h>
#include <string.h>

int longestPalindromeLength(char* s) {
    int n = strlen(s);
    int maxLen = 1;
    int start = 0;
    for (int i = 0; i < n; i++) {
        int left = i, right = i;
        while (left >= 0 && right < n && s[left] == s[right]) {
            if (right - left + 1 > maxLen) {
                maxLen = right - left + 1;
                start = left;
            }
            left--;
            right++;
        }
        left = i; right = i + 1;
        while (left >= 0 && right < n && s[left] == s[right]) {
            if (right - left + 1 > maxLen) {
                maxLen = right - left + 1;
                start = left;
            }
            left--;
            right++;
        }
    }
    return maxLen;
}

int main() {
    char s[] = "babad";
    printf("%d\n", longestPalindromeLength(s));
    return 0;
}
A3
B2
C4
D5
Attempts:
2 left
💡 Hint
Check palindromes centered at each character and between characters.
🧠 Conceptual
intermediate
1:30remaining
Understanding palindrome expansion
In the approach to find the longest palindromic substring by expanding around centers, why do we check two centers for each index?
ATo check palindromes only at even indices
BTo speed up the algorithm by parallel processing
CTo handle both odd and even length palindromes
DTo avoid checking the first and last characters
Attempts:
2 left
💡 Hint
Think about palindrome lengths like 'aba' vs 'abba'.
🔧 Debug
advanced
2:00remaining
Identify the error in palindrome substring code
What error will this code produce when run, and why? char* longestPalindrome(char* s) { int n = strlen(s); int maxLen = 1, start = 0; for (int i = 0; i < n; i++) { int left = i, right = i; while (left >= 0 && right < n && s[left] == s[right]) { if (right - left + 1 > maxLen) { maxLen = right - left + 1; start = left; } left--; right++; } } s[start + maxLen] = '\0'; return s + start; } int main() { char s[] = "cbbd"; printf("%s\n", longestPalindrome(s)); return 0; }
AIt returns a pointer to a local variable causing undefined behavior
BIt modifies the input string and may truncate original data incorrectly
CIt prints the entire original string without truncation
DIt causes a segmentation fault due to invalid pointer arithmetic
Attempts:
2 left
💡 Hint
Look at how the string is modified before returning.
Predict Output
advanced
1:30remaining
Output of Manacher's algorithm partial array
Given the input string "abacdfgdcaba", what is the value of the palindrome radius array P at index 5 after running Manacher's algorithm?
DSA C
/* Manacher's algorithm partial snippet */
int P[] = {0,1,0,3,0,1,0,1,0,3,0,1,0};
// Index 5 corresponds to character 'f' in the string
// What is P[5]?
A1
B0
C3
D2
Attempts:
2 left
💡 Hint
P[i] stores the radius of palindrome centered at i including i.
🚀 Application
expert
2:30remaining
Longest palindromic substring in a large string
You have a string of length 1,000,000 characters. Which approach is best to find the longest palindromic substring efficiently?
ABrute force checking all substrings (O(n^3))
BDynamic programming with a 2D table (O(n^2) time and space)
CExpand around centers for each character and pair (O(n^2))
DManacher's algorithm (O(n) time and space)
Attempts:
2 left
💡 Hint
Consider time complexity for very large input.