0
0
DSA Pythonprogramming~20 mins

Palindrome Detection 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 Palindrome Check Function
What is the output of the following Python code that checks if a string is a palindrome?
DSA Python
def is_palindrome(s):
    return s == s[::-1]

print(is_palindrome('radar'))
print(is_palindrome('hello'))
AFalse\nFalse
BFalse\nTrue
CTrue\nTrue
DTrue\nFalse
Attempts:
2 left
💡 Hint
Remember that s[::-1] reverses the string s.
🧠 Conceptual
intermediate
1:30remaining
Understanding Palindrome Properties
Which of the following statements about palindromes is TRUE?
AA palindrome cannot contain numbers or special characters.
BA palindrome must be a word with an even number of letters.
CA palindrome reads the same forwards and backwards ignoring spaces and punctuation.
DA palindrome is always a single word without spaces.
Attempts:
2 left
💡 Hint
Think about famous palindrome phrases like 'A man, a plan, a canal, Panama'.
Predict Output
advanced
2:30remaining
Output of Palindrome Check with Filtering
What is the output of this code that checks if a phrase is a palindrome ignoring case and non-alphanumeric characters?
DSA Python
import re

def is_palindrome_phrase(phrase):
    filtered = re.sub(r'[^a-zA-Z0-9]', '', phrase).lower()
    return filtered == filtered[::-1]

print(is_palindrome_phrase('No lemon, no melon'))
print(is_palindrome_phrase('Hello, World!'))
AFalse\nFalse
BTrue\nFalse
CTrue\nTrue
DFalse\nTrue
Attempts:
2 left
💡 Hint
The code removes all characters except letters and numbers and converts to lowercase before checking.
🔧 Debug
advanced
2:00remaining
Identify the Error in Palindrome Function
What error will this code produce when checking if a string is a palindrome?
DSA Python
def check_palindrome(s):
    for i in range(len(s)//2):
        if s[i] != s[len(s)-i]:
            return False
    return True

print(check_palindrome('level'))
AIndexError: string index out of range
BReturns False incorrectly for palindrome strings
CSyntaxError due to missing colon
DReturns True incorrectly for non-palindrome strings
Attempts:
2 left
💡 Hint
Check the indexing inside the loop carefully.
🚀 Application
expert
3:00remaining
Count Palindromic Substrings in a String
Given a string, how many palindromic substrings does it contain? Consider substrings of length 1 or more.
DSA Python
def count_palindromic_substrings(s):
    count = 0
    for i in range(len(s)):
        # Odd length palindromes
        left, right = i, i
        while left >= 0 and right < len(s) and s[left] == s[right]:
            count += 1
            left -= 1
            right += 1
        # Even length palindromes
        left, right = i, i + 1
        while left >= 0 and right < len(s) and s[left] == s[right]:
            count += 1
            left -= 1
            right += 1
    return count

print(count_palindromic_substrings('aaa'))
A6
B3
C4
D5
Attempts:
2 left
💡 Hint
Count all substrings that read the same forwards and backwards, including single letters.