Challenge - 5 Problems
Palindrome Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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'))
Attempts:
2 left
💡 Hint
Remember that s[::-1] reverses the string s.
✗ Incorrect
The function returns True if the string is the same forwards and backwards. 'radar' is a palindrome, 'hello' is not.
🧠 Conceptual
intermediate1:30remaining
Understanding Palindrome Properties
Which of the following statements about palindromes is TRUE?
Attempts:
2 left
💡 Hint
Think about famous palindrome phrases like 'A man, a plan, a canal, Panama'.
✗ Incorrect
Palindromes can ignore spaces and punctuation when checking equality, so statement B is true.
❓ Predict Output
advanced2: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!'))
Attempts:
2 left
💡 Hint
The code removes all characters except letters and numbers and converts to lowercase before checking.
✗ Incorrect
'No lemon, no melon' is a palindrome phrase ignoring spaces and punctuation, 'Hello, World!' is not.
🔧 Debug
advanced2: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'))
Attempts:
2 left
💡 Hint
Check the indexing inside the loop carefully.
✗ Incorrect
The code uses s[len(s)-i] which goes out of range because string indices go from 0 to len(s)-1.
🚀 Application
expert3: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'))
Attempts:
2 left
💡 Hint
Count all substrings that read the same forwards and backwards, including single letters.
✗ Incorrect
The substrings are: 'a', 'a', 'a', 'aa', 'aa', 'aaa' totaling 6.