Challenge - 5 Problems
Palindrome Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of palindrome check with two pointers
What is the output of the following Go code that checks if a string is a palindrome ignoring non-alphanumeric characters and case?
DSA Go
package main import ( "fmt" "unicode" ) func isPalindrome(s string) bool { left, right := 0, len(s)-1 for left < right { if !unicode.IsLetter(rune(s[left])) && !unicode.IsDigit(rune(s[left])) { left++ continue } if !unicode.IsLetter(rune(s[right])) && !unicode.IsDigit(rune(s[right])) { right-- continue } if unicode.ToLower(rune(s[left])) != unicode.ToLower(rune(s[right])) { return false } left++ right-- } return true } func main() { fmt.Println(isPalindrome("A man, a plan, a canal: Panama")) }
Attempts:
2 left
💡 Hint
Focus on how the two pointers skip non-alphanumeric characters and compare letters ignoring case.
✗ Incorrect
The code uses two pointers moving inward, skipping non-alphanumeric characters, and compares characters case-insensitively. The input string is a classic palindrome phrase ignoring punctuation and spaces, so the function returns true.
❓ Predict Output
intermediateResult of palindrome check on non-palindrome string
What does the following Go code print when checking if "race a car" is a palindrome?
DSA Go
package main import ( "fmt" "unicode" ) func isPalindrome(s string) bool { left, right := 0, len(s)-1 for left < right { if !unicode.IsLetter(rune(s[left])) && !unicode.IsDigit(rune(s[left])) { left++ continue } if !unicode.IsLetter(rune(s[right])) && !unicode.IsDigit(rune(s[right])) { right-- continue } if unicode.ToLower(rune(s[left])) != unicode.ToLower(rune(s[right])) { return false } left++ right-- } return true } func main() { fmt.Println(isPalindrome("race a car")) }
Attempts:
2 left
💡 Hint
Check the characters compared after skipping spaces.
✗ Incorrect
The string "race a car" is not a palindrome when ignoring spaces and case because the characters compared at the middle differ.
🧠 Conceptual
advancedWhy use two pointers for palindrome check?
Why is the two-pointer technique efficient for checking if a string is a palindrome ignoring non-alphanumeric characters and case?
Attempts:
2 left
💡 Hint
Think about how two pointers move and skip characters.
✗ Incorrect
Two pointers start from the beginning and end, moving inward and skipping non-alphanumeric characters. This avoids extra space and checks only necessary characters in one pass, making it O(n).
🔧 Debug
advancedIdentify the bug in palindrome check code
What error will the following Go code produce when checking if "No lemon, no melon" is a palindrome?
DSA Go
package main import ( "fmt" "unicode" ) func isPalindrome(s string) bool { left, right := 0, len(s)-1 for left < right { if !unicode.IsLetter(rune(s[left])) { left++ continue } if !unicode.IsLetter(rune(s[right])) { right-- continue } if unicode.ToLower(rune(s[left])) != unicode.ToLower(rune(s[right])) { return false } left++ right-- } return true } func main() { fmt.Println(isPalindrome("No lemon, no melon")) }
Attempts:
2 left
💡 Hint
Check what happens when the code encounters a comma or space on the right pointer.
✗ Incorrect
The code only skips letters but does not skip digits or other characters. The comma and space are not letters, so the right pointer decrements correctly. However, the code does not check digits or other non-alphanumeric characters, so it may cause incorrect comparisons or index errors if input contains digits or other characters.
🚀 Application
expertCount palindromic substrings using two pointers
Given a string, which approach correctly counts all palindromic substrings using the two-pointer technique?
Attempts:
2 left
💡 Hint
Think about how two pointers can expand outwards from centers to find palindromes.
✗ Incorrect
Counting palindromic substrings can be done by expanding around each character (odd length) and each pair (even length) using two pointers moving outward while characters match.
