Bird
Raised Fist0
DSA Cprogramming~20 mins

Palindrome Partitioning DP Minimum Cuts in DSA C - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Palindrome Partitioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of minimum cuts for palindrome partitioning
What is the output of the following code that calculates the minimum cuts needed to partition the string into palindromes?
DSA C
int minCut(char* s) {
    int n = strlen(s);
    int dp[n+1];
    int pal[n][n];
    for (int i = 0; i < n; i++) {
        dp[i] = n - 1 - i;
        for (int j = 0; j <= i; j++) {
            if (s[i] == s[j] && (i - j < 2 || pal[j+1][i-1])) {
                pal[j][i] = 1;
                if (j == 0) dp[i] = 0;
                else if (dp[j-1] + 1 < dp[i]) dp[i] = dp[j-1] + 1;
            } else {
                pal[j][i] = 0;
            }
        }
    }
    return dp[n-1];
}

// Input string: "aab" 
// Call: minCut("aab")
A1
B0
C2
D3
Attempts:
2 left
💡 Hint
Think about how many cuts are needed to split "aab" into palindromes.
🧠 Conceptual
intermediate
1:30remaining
Understanding the DP array in palindrome partitioning
In the palindrome partitioning minimum cuts problem, what does the dp array represent?
Adp[i] stores the maximum palindrome length ending at i
Bdp[i] stores the minimum cuts needed for substring s[i..end]
Cdp[i] stores the count of palindromes in s[0..i]
Ddp[i] stores the minimum cuts needed for substring s[0..i]
Attempts:
2 left
💡 Hint
Think about what the problem asks: minimum cuts for prefixes of the string.
🔧 Debug
advanced
2:00remaining
Identify the error in palindrome partitioning code
What error will the following code produce when run?
DSA C
int minCut(char* s) {
    int n = strlen(s);
    int dp[n];
    int pal[n][n];
    for (int i = 0; i < n; i++) {
        dp[i] = n - i;
        for (int j = 0; j <= i; j++) {
            if (s[i] == s[j] && (i - j < 2 || pal[j+1][i-1])) {
                pal[j][i] = 1;
                if (j == 0) dp[i] = 0;
                else if (dp[j-1] + 1 < dp[i]) dp[i] = dp[j-1] + 1;
            } else {
                pal[j][i] = 0;
            }
        }
    }
    return dp[n-1];
}

// Input: "aab"
ACompilation error due to uninitialized pal array
BArray index out of bounds error
CReturns incorrect minimum cuts
DNo error, returns correct result
Attempts:
2 left
💡 Hint
Check the initialization and indexing of dp array.
❓ Predict Output
advanced
2:30remaining
Output of minimum cuts for palindrome partitioning with repeated characters
What is the output of the minimum cuts function for the input string "ababbbabbababa"?
DSA C
int minCut(char* s) {
    int n = strlen(s);
    int dp[n];
    int pal[n][n];
    for (int i = 0; i < n; i++) {
        dp[i] = i;
        for (int j = 0; j <= i; j++) {
            if (s[i] == s[j] && (i - j < 2 || pal[j+1][i-1])) {
                pal[j][i] = 1;
                if (j == 0) dp[i] = 0;
                else if (dp[j-1] + 1 < dp[i]) dp[i] = dp[j-1] + 1;
            } else {
                pal[j][i] = 0;
            }
        }
    }
    return dp[n-1];
}

// Call: minCut("ababbbabbababa")
A3
B4
C2
D5
Attempts:
2 left
💡 Hint
Try to find the minimum cuts to split the string into palindromes.
🧠 Conceptual
expert
1:30remaining
Time complexity of palindrome partitioning minimum cuts algorithm
What is the time complexity of the dynamic programming solution for palindrome partitioning minimum cuts problem?
AO(n log n)
BO(n^2)
CO(n^3)
DO(n)
Attempts:
2 left
💡 Hint
Consider the nested loops and palindrome checks.

Practice

(1/5)
1. What does the term minimum cuts mean in the context of palindrome partitioning of a string?
easy
A. The total number of substrings in the string
B. The smallest number of cuts needed to split the string into palindromic substrings
C. The length of the longest palindromic substring
D. The maximum number of palindromic substrings in the string

Solution

  1. Step 1: Understand palindrome partitioning

    Palindrome partitioning means splitting a string into parts where each part is a palindrome.
  2. Step 2: Define minimum cuts

    Minimum cuts is the least number of cuts needed to divide the string so that every piece is a palindrome.
  3. Final Answer:

    The smallest number of cuts needed to split the string into palindromic substrings -> Option B
  4. Quick Check:

    Minimum cuts = smallest cuts for palindrome parts [OK]
Hint: Minimum cuts split string into palindrome parts [OK]
Common Mistakes:
  • Confusing minimum cuts with maximum palindromes
  • Thinking minimum cuts means longest palindrome length
  • Counting total substrings instead of cuts
2. Which of the following is the correct way to declare a 2D boolean array isPalindrome of size n x n in C for palindrome checks?
easy
A. bool isPalindrome[n][n];
B. bool isPalindrome = new bool[n][n];
C. bool* isPalindrome = malloc(n * n * sizeof(bool));
D. bool isPalindrome[n];

Solution

  1. Step 1: Understand 2D array declaration in C

    In C, a 2D array of booleans with size n x n is declared as bool isPalindrome[n][n];
  2. Step 2: Check other options

    bool isPalindrome = new bool[n][n]; uses Java-like syntax, bool* isPalindrome = malloc(n * n * sizeof(bool)); is a pointer but not a 2D array, bool isPalindrome[n]; is 1D array.
  3. Final Answer:

    bool isPalindrome[n][n]; -> Option A
  4. Quick Check:

    2D array in C = bool isPalindrome[n][n]; [OK]
Hint: Use bool isPalindrome[n][n]; for 2D boolean arrays in C [OK]
Common Mistakes:
  • Using Java or C++ syntax in C
  • Declaring 1D array instead of 2D
  • Using malloc without proper pointer handling
3. Consider the following C code snippet for minimum cuts palindrome partitioning. What is the output for input string "aab"?
int minCut(char* s) {
    int n = strlen(s);
    int cuts[n+1];
    bool isPal[n][n];
    for (int i = 0; i < n; i++) {
        cuts[i] = i - 1;
        for (int j = 0; j <= i; j++) {
            if (s[j] == s[i] && (i - j < 2 || isPal[j+1][i-1])) {
                isPal[j][i] = true;
                cuts[i] = j == 0 ? 0 : fmin(cuts[i], cuts[j-1] + 1);
            } else {
                isPal[j][i] = false;
            }
        }
    }
    return cuts[n-1];
}
printf("%d", minCut("aab"));
medium
A. 2
B. 0
C. 1
D. 3

Solution

  1. Step 1: Trace palindrome checks and cuts for "aab"

    For i=0 ('a'): cuts[0]= -1 initially, palindrome is 'a', cuts[0]=0.
    For i=1 ('a'): substring 'aa' is palindrome, cuts[1]=0.
    For i=2 ('b'): 'b' is palindrome, cuts[2] = min(cuts[2], cuts[1]+1) = 1.
  2. Step 2: Final minimum cuts calculation

    Minimum cuts needed to partition "aab" into palindromes is 1 (split as "aa" | "b").
  3. Final Answer:

    1 -> Option C
  4. Quick Check:

    Minimum cuts for "aab" = 1 [OK]
Hint: Trace cuts array updates for each palindrome end [OK]
Common Mistakes:
  • Assuming no cuts needed for non-palindromic parts
  • Misunderstanding substring palindrome checks
  • Incorrectly initializing cuts array
4. The following code snippet for palindrome partitioning minimum cuts has a bug. What is the error?
int minCut(char* s) {
    int n = strlen(s);
    int cuts[n];
    bool isPal[n][n];
    for (int i = 0; i < n; i++) {
        cuts[i] = i;
        for (int j = 0; j <= i; j++) {
            if (s[j] == s[i] && (i - j < 2 || isPal[j+1][i-1])) {
                isPal[j][i] = true;
                cuts[i] = j == 0 ? 0 : fmin(cuts[i], cuts[j-1] + 1);
            } else {
                isPal[j][i] = false;
            }
        }
    }
    return cuts[n-1];
}
medium
A. The condition (i - j < 2) is incorrect
B. isPal array is not initialized before use
C. The function returns cuts[0] instead of cuts[n-1]
D. cuts array should be of size n+1 and initialized with i-1

Solution

  1. Step 1: Check cuts array size and initialization

    cuts array should be size n+1 to handle base case and initialized with i-1 to represent max cuts.
  2. Step 2: Identify impact of incorrect cuts initialization

    Initializing cuts[i] = i causes wrong minimum cuts calculation because it doesn't consider zero-based indexing properly.
  3. Final Answer:

    cuts array should be of size n+1 and initialized with i-1 -> Option D
  4. Quick Check:

    Correct cuts size and init = n+1 and i-1 [OK]
Hint: Initialize cuts with i-1 and size n+1 [OK]
Common Mistakes:
  • Using cuts size n instead of n+1
  • Not initializing cuts properly
  • Ignoring base case for cuts
5. Given the string "abcbm", what is the minimum number of cuts needed to partition it into palindromic substrings using dynamic programming?
hard
A. 2
B. 1
C. 3
D. 0

Solution

  1. Step 1: Identify palindromic substrings in "abcbm"

    "a" is palindrome, "bcb" is palindrome, "m" is palindrome.
  2. Step 2: Calculate minimum cuts

    Partition as "a" | "bcb" | "m" requires 2 cuts.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Minimum cuts for "abcbm" = 2 [OK]
Hint: Look for longest palindromes to reduce cuts [OK]
Common Mistakes:
  • Counting cuts as number of palindromes
  • Missing middle palindrome "bcb"
  • Assuming zero cuts for non-palindromic string
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard