0
0
DSA Cprogramming~20 mins

Palindrome Partitioning Using Backtracking in DSA C - Practice Problems & Challenges

Choose your learning style9 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 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;
}
A
a|ab
aa|b
B
a|a|b
aab
C
a|a|b
aa|b
D
aa|b
a|b
Attempts:
2 left
💡 Hint
Check all substrings that are palindromes and print partitions separated by '|'.
🧠 Conceptual
intermediate
1:00remaining
Number of Palindrome Partitions for "abc"
How many palindrome partitions does the string "abc" have?
A0
B1
C2
D3
Attempts:
2 left
💡 Hint
Each single character is a palindrome itself.
Predict Output
advanced
3: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;
}
A
r|a|c|e|c|a|r
r|a|cec|a|r
r|aceca|r
racecar
B
r|a|c|e|c|a|r
ra|cec|ar
racecar
C
r|a|c|e|c|a|r
r|aceca|r
racecar
D
racecar
r|a|c|e|c|a|r
r|a|cec|a|r
Attempts:
2 left
💡 Hint
Look for all palindrome substrings including single letters and longer palindromes like "cec", "aceca", and "racecar" itself.
🔧 Debug
advanced
1: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);
ACauses infinite loop
BReturns false without error
CCauses out-of-bounds access
DReturns true without error
Attempts:
2 left
💡 Hint
Check the while loop condition and what happens if start is greater than end initially.
🚀 Application
expert
3:00remaining
Minimum Cuts for Palindrome Partitioning
Given the string "ababbbabbababa", what is the minimum number of cuts needed to partition it into palindromes?
A3
B5
C4
D6
Attempts:
2 left
💡 Hint
Use dynamic programming or backtracking to find minimum cuts for palindrome partitions.