Challenge - 5 Problems
Palindrome Partitioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Palindrome Partitioning for 'aab'
What is the output of the following Go code that finds all palindrome partitions of the string 'aab'?
DSA Go
package main import ( "fmt" ) func isPalindrome(s string) bool { for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { if s[i] != s[j] { return false } } return true } func backtrack(s string, start int, path []string, res *[][]string) { if start == len(s) { temp := make([]string, len(path)) copy(temp, path) *res = append(*res, temp) return } for end := start + 1; end <= len(s); end++ { if isPalindrome(s[start:end]) { backtrack(s, end, append(path, s[start:end]), res) } } } func partition(s string) [][]string { var res [][]string backtrack(s, 0, []string{}, &res) return res } func main() { result := partition("aab") fmt.Println(result) }
Attempts:
2 left
💡 Hint
Think about all ways to split the string where each part is a palindrome.
✗ Incorrect
The code finds all partitions of 'aab' where each substring is a palindrome. 'a', 'a', 'b' and 'aa', 'b' are the valid palindrome partitions.
🧠 Conceptual
intermediateUnderstanding Backtracking in Palindrome Partitioning
Which statement best describes the role of backtracking in palindrome partitioning?
Attempts:
2 left
💡 Hint
Backtracking explores all options but only keeps valid ones.
✗ Incorrect
Backtracking explores all substring partitions and only keeps those where each substring is a palindrome, discarding others.
🔧 Debug
advancedIdentify the Error in Palindrome Partitioning Code
What error will the following Go code produce when run?
DSA Go
package main import "fmt" func isPalindrome(s string) bool { for i := 0; i < len(s)/2; i++ { if s[i] != s[len(s)-i-1] { return false } } return true } func main() { fmt.Println(isPalindrome("abba")) }
Attempts:
2 left
💡 Hint
Check the index used for the right side character.
✗ Incorrect
The code uses s[len(s)-i] which is out of range because string indices go from 0 to len(s)-1.
🚀 Application
advancedNumber of Palindrome Partitions for 'racecar'
How many palindrome partitions does the string 'racecar' have?
Attempts:
2 left
💡 Hint
Consider all ways to split 'racecar' into palindromes like 'r', 'a', 'cec', 'a', 'r'.
✗ Incorrect
The string 'racecar' has 8 palindrome partitions including single letters and larger palindromes like 'cec' and 'aceca'.
❓ Predict Output
expertOutput of Palindrome Partitioning with Memoization
What is the output of the following Go code that uses memoization to optimize palindrome partitioning for 'aaa'?
DSA Go
package main import "fmt" func isPalindrome(s string) bool { for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { if s[i] != s[j] { return false } } return true } func backtrack(s string, start int, memo map[int][][]string) [][]string { if val, ok := memo[start]; ok { return val } var res [][]string if start == len(s) { return [][]string{{}} } for end := start + 1; end <= len(s); end++ { if isPalindrome(s[start:end]) { for _, sub := range backtrack(s, end, memo) { partition := append([]string{s[start:end]}, sub...) res = append(res, partition) } } } memo[start] = res return res } func partition(s string) [][]string { memo := make(map[int][][]string) return backtrack(s, 0, memo) } func main() { result := partition("aaa") fmt.Println(result) }
Attempts:
2 left
💡 Hint
Memoization caches results to avoid repeated work.
✗ Incorrect
The code returns all palindrome partitions of 'aaa' including single letters, pairs, and the whole string.
