0
0
DSA Pythonprogramming~20 mins

Longest Palindromic Substring in DSA Python - 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 the following code snippet that finds the length of the longest palindromic substring in the given string?
DSA Python
def longest_palindrome_length(s):
    max_len = 1
    for i in range(len(s)):
        # Odd length palindrome
        l, r = i, i
        while l >= 0 and r < len(s) and s[l] == s[r]:
            max_len = max(max_len, r - l + 1)
            l -= 1
            r += 1
        # Even length palindrome
        l, r = i, i + 1
        while l >= 0 and r < len(s) and s[l] == s[r]:
            max_len = max(max_len, r - l + 1)
            l -= 1
            r += 1
    return max_len

print(longest_palindrome_length("babad"))
A3
B2
C4
D5
Attempts:
2 left
💡 Hint
Check palindromes centered at each character and between characters.
🧠 Conceptual
intermediate
1:30remaining
Understanding palindrome expansion technique
Why does the algorithm check palindromes by expanding around each character and also between characters?
ATo skip checking substrings that are not palindromes
BTo handle only odd length palindromes
CTo optimize time complexity to O(n)
DTo find both odd and even length palindromes
Attempts:
2 left
💡 Hint
Think about palindrome lengths and their centers.
Predict Output
advanced
2:00remaining
Output of longest palindromic substring
What is the output of this code that returns the longest palindromic substring itself?
DSA Python
def longest_palindrome_substring(s):
    start, max_len = 0, 1
    for i in range(len(s)):
        # Odd length
        l, r = i, i
        while l >= 0 and r < len(s) and s[l] == s[r]:
            if (r - l + 1) > max_len:
                start = l
                max_len = r - l + 1
            l -= 1
            r += 1
        # Even length
        l, r = i, i + 1
        while l >= 0 and r < len(s) and s[l] == s[r]:
            if (r - l + 1) > max_len:
                start = l
                max_len = r - l + 1
            l -= 1
            r += 1
    return s[start:start+max_len]

print(longest_palindrome_substring("cbbd"))
A"bb"
B"cbbd"
C"b"
D"c"
Attempts:
2 left
💡 Hint
Look for the longest substring that reads the same forwards and backwards.
🔧 Debug
advanced
1:30remaining
Identify the error in palindrome substring code
What error will this code produce when run?
DSA Python
def longest_palindrome_substring(s):
    start, max_len = 0, 1
    for i in range(len(s)):
        l, r = i, i
        while l >= 0 and r < len(s) and s[l] == s[r]:
            if r - l + 1 > max_len:
                start = l
                max_len = r - l + 1
            l -= 1
            r += 1
        l, r = i, i + 1
        while l >= 0 and r < len(s) and s[l] == s[r]:
            if r - l + 1 > max_len:
                start = l
                max_len = r - l + 1
            l -= 1
            r += 1
    return s[start:start+max_len]

print(longest_palindrome_substring("abba"))
ATypeError due to wrong comparison
BIndexError due to out of range
CSyntaxError due to missing colon
DNo error, outputs "abba"
Attempts:
2 left
💡 Hint
Check the line with the if statement inside the second while loop.
🚀 Application
expert
2:30remaining
Longest palindromic substring count in multiple strings
Given a list of strings, how many of them have a longest palindromic substring of length at least 3? Strings: ["racecar", "abc", "noon", "xyzzyx", "a"]
DSA Python
def longest_palindrome_length(s):
    max_len = 1
    for i in range(len(s)):
        l, r = i, i
        while l >= 0 and r < len(s) and s[l] == s[r]:
            max_len = max(max_len, r - l + 1)
            l -= 1
            r += 1
        l, r = i, i + 1
        while l >= 0 and r < len(s) and s[l] == s[r]:
            max_len = max(max_len, r - l + 1)
            l -= 1
            r += 1
    return max_len

strings = ["racecar", "abc", "noon", "xyzzyx", "a"]
count = 0
for s in strings:
    if longest_palindrome_length(s) >= 3:
        count += 1
print(count)
A4
B3
C2
D5
Attempts:
2 left
💡 Hint
Check each string's longest palindrome length carefully.