0
0
DSA Pythonprogramming~20 mins

Valid Palindrome Two Pointer in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Palindrome Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Two Pointer Palindrome Check
What is the output of the following code when checking if the string is a valid palindrome ignoring non-alphanumeric characters and case?
DSA Python
def is_palindrome(s: str) -> bool:
    left, right = 0, len(s) - 1
    while left < right:
        while left < right and not s[left].isalnum():
            left += 1
        while left < right and not s[right].isalnum():
            right -= 1
        if s[left].lower() != s[right].lower():
            return False
        left += 1
        right -= 1
    return True

print(is_palindrome("A man, a plan, a canal: Panama"))
AFalse
BSyntaxError
CTypeError
DTrue
Attempts:
2 left
💡 Hint
Check how non-alphanumeric characters are skipped and case is ignored.
Predict Output
intermediate
2:00remaining
Output for Non-Palindrome String
What will the function return for the input string "race a car"?
DSA Python
def is_palindrome(s: str) -> bool:
    left, right = 0, len(s) - 1
    while left < right:
        while left < right and not s[left].isalnum():
            left += 1
        while left < right and not s[right].isalnum():
            right -= 1
        if s[left].lower() != s[right].lower():
            return False
        left += 1
        right -= 1
    return True

print(is_palindrome("race a car"))
AFalse
BTrue
CIndexError
DValueError
Attempts:
2 left
💡 Hint
Check the characters compared at the first mismatch.
🔧 Debug
advanced
2:00remaining
Identify the bug in palindrome check
What error will this code produce when run with input "No lemon, no melon"?
DSA Python
def is_palindrome(s: str) -> bool:
    left, right = 0, len(s) - 1
    while left <= right:
        if not s[left].isalnum():
            left += 1
        if not s[right].isalnum():
            right -= 1
        if s[left].lower() != s[right].lower():
            return False
        left += 1
        right -= 1
    return True

print(is_palindrome("No lemon, no melon"))
AIndexError
BTrue
CFalse
DSyntaxError
Attempts:
2 left
💡 Hint
Check how pointers move when skipping characters.
Predict Output
advanced
2:00remaining
Output of palindrome check with empty string
What is the output of the palindrome check function when the input string is empty?
DSA Python
def is_palindrome(s: str) -> bool:
    left, right = 0, len(s) - 1
    while left < right:
        while left < right and not s[left].isalnum():
            left += 1
        while left < right and not s[right].isalnum():
            right -= 1
        if s[left].lower() != s[right].lower():
            return False
        left += 1
        right -= 1
    return True

print(is_palindrome("") )
AIndexError
BFalse
CTrue
DTypeError
Attempts:
2 left
💡 Hint
Consider the meaning of an empty string as a palindrome.
🧠 Conceptual
expert
2:00remaining
Why use two pointers for palindrome check?
Which of the following best explains why the two-pointer technique is efficient for checking if a string is a palindrome ignoring non-alphanumeric characters and case?
AIt converts the string to a list and reverses it, then compares both lists for equality.
BIt allows checking characters from both ends simultaneously, skipping irrelevant characters, reducing time complexity to O(n).
CIt uses recursion to compare characters, which uses less memory than iteration.
DIt sorts the string first, then compares characters, which is faster than other methods.
Attempts:
2 left
💡 Hint
Think about how skipping characters and comparing from both ends helps.