Challenge - 5 Problems
Palindrome Partitioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Palindrome Partitioning for "aab"
What is the output of the following C code that prints all palindrome partitions of the string "aab"?
DSA C
#include <stdio.h> #include <string.h> #include <stdbool.h> bool isPalindrome(char *str, int start, int end) { while (start < end) { if (str[start] != str[end]) return false; start++; end--; } return true; } void backtrack(char *s, int start, int len, char result[][100], int resultSize) { if (start == len) { for (int i = 0; i < resultSize; i++) { printf("%s", result[i]); if (i != resultSize - 1) printf("|"); } printf("\n"); return; } for (int end = start; end < len; end++) { if (isPalindrome(s, start, end)) { int substrLen = end - start + 1; strncpy(result[resultSize], s + start, substrLen); result[resultSize][substrLen] = '\0'; backtrack(s, end + 1, len, result, resultSize + 1); } } } int main() { char s[] = "aab"; int len = strlen(s); char result[100][100]; backtrack(s, 0, len, result, 0); return 0; }
Attempts:
2 left
💡 Hint
Check all substrings that are palindromes and print partitions separated by '|'.
✗ Incorrect
The code finds all palindrome partitions of "aab". The partitions are: "a|a|b" and "aa|b".
🧠 Conceptual
intermediate1:00remaining
Number of Palindrome Partitions for "abc"
How many palindrome partitions does the string "abc" have?
Attempts:
2 left
💡 Hint
Each single character is a palindrome itself.
✗ Incorrect
The only palindrome partition is "a|b|c", since no consecutive characters form a palindrome. Thus, there is 1 palindrome partition.
❓ Predict Output
advanced3:00remaining
Output of Palindrome Partitioning for "racecar"
What is the output of the following C code that prints all palindrome partitions of the string "racecar"?
DSA C
#include <stdio.h> #include <string.h> #include <stdbool.h> bool isPalindrome(char *str, int start, int end) { while (start < end) { if (str[start] != str[end]) return false; start++; end--; } return true; } void backtrack(char *s, int start, int len, char result[][100], int resultSize) { if (start == len) { for (int i = 0; i < resultSize; i++) { printf("%s", result[i]); if (i != resultSize - 1) printf("|"); } printf("\n"); return; } for (int end = start; end < len; end++) { if (isPalindrome(s, start, end)) { int substrLen = end - start + 1; strncpy(result[resultSize], s + start, substrLen); result[resultSize][substrLen] = '\0'; backtrack(s, end + 1, len, result, resultSize + 1); } } } int main() { char s[] = "racecar"; int len = strlen(s); char result[100][100]; backtrack(s, 0, len, result, 0); return 0; }
Attempts:
2 left
💡 Hint
Look for all palindrome substrings including single letters and longer palindromes like "cec", "aceca", and "racecar" itself.
✗ Incorrect
The code prints all palindrome partitions of "racecar". The partitions include all single letters, and longer palindromes like "cec", "aceca", and the full "racecar".
🔧 Debug
advanced1:30remaining
Identify the error in palindrome check function
What error will the following palindrome check function cause when called with start > end?
DSA C
bool isPalindrome(char *str, int start, int end) { while (start < end) { if (str[start] != str[end]) return false; start++; end--; } return true; } // Called as isPalindrome(s, 3, 2);
Attempts:
2 left
💡 Hint
Check the while loop condition and what happens if start is greater than end initially.
✗ Incorrect
If start > end initially, the while loop condition is false, so the function returns true immediately. This is correct behavior for empty or invalid substring.
🚀 Application
expert3:00remaining
Minimum Cuts for Palindrome Partitioning
Given the string "ababbbabbababa", what is the minimum number of cuts needed to partition it into palindromes?
Attempts:
2 left
💡 Hint
Use dynamic programming or backtracking to find minimum cuts for palindrome partitions.
✗ Incorrect
The minimum cuts needed for "ababbbabbababa" is 3. One optimal partition is: "a|babbbab|b|ababa".