Bird
Raised Fist0
DSA Cprogramming~20 mins

Palindrome Partitioning Using Backtracking 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 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.

Practice

(1/5)
1. What is the main goal of palindrome partitioning using backtracking on a string s?
easy
A. To find the longest palindrome substring in s
B. To split s into all possible substrings where each substring is a palindrome
C. To count the number of palindromes in s
D. To reverse the string s using recursion

Solution

  1. Step 1: Understand palindrome partitioning

    Palindrome partitioning means dividing the string into parts where each part reads the same forwards and backwards.
  2. Step 2: Role of backtracking

    Backtracking helps explore all possible ways to split the string into palindrome substrings.
  3. Final Answer:

    To split s into all possible substrings where each substring is a palindrome -> Option B
  4. Quick Check:

    Palindrome partitioning = all palindrome substrings [OK]
Hint: Backtracking explores all palindrome splits [OK]
Common Mistakes:
  • Confusing palindrome partitioning with longest palindrome substring
  • Thinking it only counts palindromes instead of listing partitions
  • Assuming it reverses the string
2. Which of the following is the correct base case condition in a backtracking function for palindrome partitioning of string s starting at index start?
easy
A. if (start == s.length()) add current partition to result and return;
B. if (start > s.length()) return false;
C. if (start == s.length()) return true;
D. if (start < 0) return;

Solution

  1. Step 1: Identify when to stop recursion

    When start reaches the length of the string, it means the entire string is partitioned.
  2. Step 2: What to do at base case

    At this point, add the current list of palindrome substrings to the result list and return to explore other partitions.
  3. Final Answer:

    if (start == s.length()) add current partition to result and return; -> Option A
  4. Quick Check:

    Base case adds partition when start reaches string end [OK]
Hint: Add partition when start equals string length [OK]
Common Mistakes:
  • Returning true/false instead of adding partition
  • Checking start > length instead of == length
  • Using invalid index checks like start < 0
3. What will be the output partitions for the string "aab" using palindrome partitioning with backtracking?
medium
A. [["aab"]]
B. [["aa", "ab"]]
C. [["a", "ab"]]
D. [["a", "a", "b"], ["aa", "b"]]

Solution

  1. Step 1: Check all palindrome substrings

    "a" is palindrome, "aa" is palindrome, "b" is palindrome, but "ab" and "aab" are not.
  2. Step 2: List all palindrome partitions

    Partitions are ["a", "a", "b"] and ["aa", "b"].
  3. Final Answer:

    [["a", "a", "b"], ["aa", "b"]] -> Option D
  4. Quick Check:

    Valid palindrome partitions = [["a", "a", "b"], ["aa", "b"]] [OK]
Hint: Check substrings palindrome before partitioning [OK]
Common Mistakes:
  • Including non-palindrome substrings like "ab"
  • Missing the partition ["aa", "b"]
  • Returning the whole string as one partition
4. Given the following code snippet for palindrome partitioning, what is the error?
void backtrack(char* s, int start, char** current, int currentSize, char*** result, int* returnSize) {
  if (start == strlen(s)) {
    result[*returnSize] = current;
    (*returnSize)++;
    return;
  }
  for (int end = start; end < strlen(s); end++) {
    if (isPalindrome(s, start, end)) {
      current[currentSize] = substring(s, start, end);
      backtrack(s, end + 1, current, currentSize + 1, result, returnSize);
    }
  }
}
medium
A. Assigning current directly to result causes shared reference issues
B. The base case condition is incorrect
C. The loop should run from end = 0 to end < strlen(s)
D. Missing return statement after recursive call

Solution

  1. Step 1: Analyze base case assignment

    Assigning current pointer directly to result[*returnSize] stores the same reference for all partitions.
  2. Step 2: Understand backtracking data handling

    We must copy the current partition array to avoid overwriting when backtracking continues.
  3. Final Answer:

    Assigning current directly to result causes shared reference issues -> Option A
  4. Quick Check:

    Copy partitions to result to avoid shared references [OK]
Hint: Copy current partition before adding to result [OK]
Common Mistakes:
  • Not copying current partition array
  • Incorrect loop range for end index
  • Ignoring base case correctness
5. How can you optimize palindrome checking in backtracking palindrome partitioning for string s of length n?
hard
A. Skip palindrome checking and add all substrings to partitions
B. Check palindrome by reversing substring every time during recursion
C. Precompute a 2D boolean table dp where dp[i][j] is true if substring s[i..j] is palindrome
D. Use a hash map to store all substrings and their palindrome status

Solution

  1. Step 1: Understand palindrome checking cost

    Checking palindrome by reversing substring each time costs O(n) per check, leading to O(n^3) overall.
  2. Step 2: Use dynamic programming optimization

    Precompute a 2D table dp where dp[i][j] stores palindrome status for substring s[i..j]. This reduces palindrome checks to O(1).
  3. Step 3: Benefit in backtracking

    Using dp speeds up backtracking by avoiding repeated palindrome checks.
  4. Final Answer:

    Precompute a 2D boolean table dp where dp[i][j] is true if substring s[i..j] is palindrome -> Option C
  5. Quick Check:

    DP table for palindrome checking optimizes backtracking [OK]
Hint: Use DP table to check palindrome in O(1) time [OK]
Common Mistakes:
  • Reversing substring every time causing slow performance
  • Using hash map adds unnecessary overhead
  • Skipping palindrome checks leads to wrong partitions
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