Challenge - 5 Problems
Palindrome Partitioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of minimum cuts for palindrome partitioning
What is the output of the following Go code that calculates the minimum cuts needed to partition a string into palindromes?
DSA Go
package main import "fmt" func minCut(s string) int { n := len(s) dp := make([]int, n+1) for i := 0; i <= n; i++ { dp[i] = i - 1 } for i := 0; i < n; i++ { for j := 0; i-j >= 0 && i+j < n && s[i-j] == s[i+j]; j++ { if dp[i-j] + 1 < dp[i+j+1] { dp[i+j+1] = dp[i-j] + 1 } } for j := 1; i-j+1 >= 0 && i+j < n && s[i-j+1] == s[i+j]; j++ { if dp[i-j+1] + 1 < dp[i+j+1] { dp[i+j+1] = dp[i-j+1] + 1 } } } return dp[n] } func main() { fmt.Println(minCut("aab")) }
Attempts:
2 left
💡 Hint
Think about how many cuts are needed to split "aab" into palindromes.
✗ Incorrect
The string "aab" can be split into "aa" and "b", both palindromes, with only 1 cut.
❓ Predict Output
intermediateMinimum cuts for palindrome partitioning of 'racecar'
What is the output of the following Go code for the input string "racecar"?
DSA Go
package main import "fmt" func minCut(s string) int { n := len(s) dp := make([]int, n+1) for i := 0; i <= n; i++ { dp[i] = i - 1 } for i := 0; i < n; i++ { for j := 0; i-j >= 0 && i+j < n && s[i-j] == s[i+j]; j++ { if dp[i-j] + 1 < dp[i+j+1] { dp[i+j+1] = dp[i-j] + 1 } } for j := 1; i-j+1 >= 0 && i+j < n && s[i-j+1] == s[i+j]; j++ { if dp[i-j+1] + 1 < dp[i+j+1] { dp[i+j+1] = dp[i-j+1] + 1 } } } return dp[n] } func main() { fmt.Println(minCut("racecar")) }
Attempts:
2 left
💡 Hint
Check if the entire string is a palindrome.
✗ Incorrect
The string "racecar" is a palindrome itself, so no cuts are needed.
🔧 Debug
advancedIdentify the runtime error in palindrome partitioning code
Which option will cause a runtime error when running the following Go code for input "abc"?
DSA Go
package main import "fmt" func minCut(s string) int { n := len(s) dp := make([]int, n+1) for i := 0; i <= n; i++ { dp[i] = i - 1 } for i := 0; i < n; i++ { for j := 0; i-j >= 0 && i+j < n && s[i-j] == s[i+j]; j++ { if dp[i-j] + 1 < dp[i+j+1] { dp[i+j+1] = dp[i-j] + 1 } } for j := 1; i-j+1 >= 0 && i+j < n && s[i-j+1] == s[i+j]; j++ { if dp[i-j+1] + 1 < dp[i+j+1] { dp[i+j+1] = dp[i-j+1] + 1 } } } return dp[n] } func main() { fmt.Println(minCut("abc")) }
Attempts:
2 left
💡 Hint
Check the dp slice indexing carefully.
✗ Incorrect
The code correctly handles dp slice indexing and outputs 2 for "abc".
🧠 Conceptual
advancedUnderstanding the DP array in palindrome partitioning
In the palindrome partitioning minimum cuts algorithm, what does the dp array represent?
Attempts:
2 left
💡 Hint
Think about how dp is initialized and updated.
✗ Incorrect
dp[i] holds minimum cuts needed for substring from start to index i-1.
🚀 Application
expertMinimum cuts for palindrome partitioning of 'abccbaabccba'
What is the minimum number of cuts needed to partition the string "abccbaabccba" into palindromes?
Attempts:
2 left
💡 Hint
Try to find large palindrome substrings to minimize cuts.
✗ Incorrect
The string can be split into "abccba" and "abccba", both palindromes, with 1 cut, but the string "abccbaabccba" is not palindrome itself, so minimum cuts is 2.
