Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 1 instead of 0 when characters don't match.
Not returning any value inside the if condition.
✗ Incorrect
Return 0 when characters at start and end do not match, meaning substring is not palindrome.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -- which decreases the index.
Using = which overwrites the index.
✗ Incorrect
Increment the result index after adding a substring to move to next position.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using start + 1 which may skip substrings.
Using end which causes infinite recursion.
✗ Incorrect
After choosing substring s[start..end], next recursion starts from end+1 to avoid overlap.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using resIndex-- which changes pointer address.
Not decrementing resIndex causing wrong partitions.
✗ Incorrect
After recursion, decrement resIndex to remove last substring, then increment to restore for next iteration.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing resIndex instead of &resIndex.
Starting backtrack from 1 instead of 0.
✗ Incorrect
Initialize resIndex to 0, start backtrack from index 0, and pass address of resIndex.