0
0
DSA Cprogramming~10 mins

Palindrome Partitioning Using Backtracking in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if a substring is a palindrome.

DSA C
int isPalindrome(char *s, int start, int end) {
    while (start < end) {
        if (s[start] != s[end]) {
            return [1];
        }
        start++;
        end--;
    }
    return 1;
}
Drag options to blanks, or click blank then click option'
A0
B1
C-1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 1 instead of 0 when characters don't match.
Not returning any value inside the if condition.
2fill in blank
medium

Complete the code to add a substring to the current partition list.

DSA C
void addSubstring(char *s, int start, int end, char result[][100], int *resIndex) {
    int i, j = 0;
    for (i = start; i <= end; i++) {
        result[*resIndex][j++] = s[i];
    }
    result[*resIndex][j] = '\0';
    (*resIndex)[1];
}
Drag options to blanks, or click blank then click option'
A++
B--
C+=
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using -- which decreases the index.
Using = which overwrites the index.
3fill in blank
hard

Fix the error in the recursive backtracking call to explore partitions.

DSA C
void backtrack(char *s, int start, char result[][100], int resIndex) {
    if (s[start] == '\0') {
        // print or store the partition
        return;
    }
    for (int end = start; s[end] != '\0'; end++) {
        if (isPalindrome(s, start, end)) {
            addSubstring(s, start, end, result, &resIndex);
            backtrack(s, [1], result, resIndex);
        }
    }
}
Drag options to blanks, or click blank then click option'
Aend
Bstart + 1
Cend + 1
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using start + 1 which may skip substrings.
Using end which causes infinite recursion.
4fill in blank
hard

Fill both blanks to correctly backtrack after recursive call.

DSA C
void backtrack(char *s, int start, char result[][100], int *resIndex) {
    if (s[start] == '\0') {
        // print or store the partition
        return;
    }
    for (int end = start; s[end] != '\0'; end++) {
        if (isPalindrome(s, start, end)) {
            addSubstring(s, start, end, result, resIndex);
            backtrack(s, end + 1, result, resIndex);
            [1];
            [2];
        }
    }
}
Drag options to blanks, or click blank then click option'
A*resIndex--
B(*resIndex)--
CresIndex--
D(*resIndex)++
Attempts:
3 left
💡 Hint
Common Mistakes
Using resIndex-- which changes pointer address.
Not decrementing resIndex causing wrong partitions.
5fill in blank
hard

Fill all three blanks to initialize and start palindrome partitioning.

DSA C
int main() {
    char s[] = "aab";
    char result[100][100];
    int [1] = 0;
    backtrack(s, [2], result, &[3]);
    return 0;
}
Drag options to blanks, or click blank then click option'
AresIndex
B0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Passing resIndex instead of &resIndex.
Starting backtrack from 1 instead of 0.