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 C# code that calculates the minimum cuts needed to partition the string into palindromes?
DSA C#
using System; class Program { static bool IsPalindrome(string s, int start, int end) { while (start < end) { if (s[start] != s[end]) return false; start++; end--; } return true; } static int MinCut(string s) { int n = s.Length; int[] dp = new int[n]; for (int i = 0; i < n; i++) { dp[i] = i; // max cuts for (int j = 0; j <= i; j++) { if (IsPalindrome(s, j, i)) { dp[i] = j == 0 ? 0 : Math.Min(dp[i], dp[j - 1] + 1); } } } return dp[n - 1]; } static void Main() { string input = "aab"; Console.WriteLine(MinCut(input)); } }
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 1 cut.
❓ Predict Output
intermediateMinimum cuts for palindrome partitioning of a longer string
What is the output of the following C# code for the input string "ababbbabbababa"?
DSA C#
using System; class Program { static bool IsPalindrome(string s, int start, int end) { while (start < end) { if (s[start] != s[end]) return false; start++; end--; } return true; } static int MinCut(string s) { int n = s.Length; int[] dp = new int[n]; for (int i = 0; i < n; i++) { dp[i] = i; for (int j = 0; j <= i; j++) { if (IsPalindrome(s, j, i)) { dp[i] = j == 0 ? 0 : Math.Min(dp[i], dp[j - 1] + 1); } } } return dp[n - 1]; } static void Main() { string input = "ababbbabbababa"; Console.WriteLine(MinCut(input)); } }
Attempts:
2 left
💡 Hint
Try to find the minimum cuts by checking palindrome substrings carefully.
✗ Incorrect
The minimum cuts for "ababbbabbababa" is 2, splitting into palindromes like "a", "babbbab", "bababa".
🧠 Conceptual
advancedUnderstanding the DP array in palindrome partitioning
In the palindrome partitioning minimum cuts algorithm, what does the dp array represent at index i?
Attempts:
2 left
💡 Hint
Think about what the algorithm tries to minimize at each position.
✗ Incorrect
dp[i] stores the minimum cuts needed to partition the substring from start to i into palindromes.
❓ Predict Output
advancedOutput of minimum cuts for a palindrome string
What is the output of the following code for the input string "racecar"?
DSA C#
using System; class Program { static bool IsPalindrome(string s, int start, int end) { while (start < end) { if (s[start] != s[end]) return false; start++; end--; } return true; } static int MinCut(string s) { int n = s.Length; int[] dp = new int[n]; for (int i = 0; i < n; i++) { dp[i] = i; for (int j = 0; j <= i; j++) { if (IsPalindrome(s, j, i)) { dp[i] = j == 0 ? 0 : Math.Min(dp[i], dp[j - 1] + 1); } } } return dp[n - 1]; } static void Main() { string input = "racecar"; Console.WriteLine(MinCut(input)); } }
Attempts:
2 left
💡 Hint
Is the entire string a palindrome?
✗ Incorrect
Since "racecar" is a palindrome itself, no cuts are needed, so output is 0.
🧠 Conceptual
expertTime complexity of palindrome partitioning minimum cuts algorithm
What is the time complexity of the standard dynamic programming solution for palindrome partitioning minimum cuts using the approach shown in the code?
Attempts:
2 left
💡 Hint
Consider the nested loops and palindrome checks.
✗ Incorrect
The algorithm has two nested loops and a palindrome check inside, leading to O(n^3) time complexity.
