0
0
DSA Cprogramming~10 mins

Recursion on Arrays and Strings 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 return the sum of elements in the array recursively.

DSA C
int sumArray(int arr[], int n) {
    if (n == 0) {
        return 0;
    }
    return arr[n - 1] + [1];
}
Drag options to blanks, or click blank then click option'
AsumArray(arr, n + 1)
BsumArray(arr, n - 1)
CsumArray(arr, n)
DsumArray(arr, 0)
Attempts:
3 left
💡 Hint
Common Mistakes
Using n+1 causes infinite recursion.
Using n causes the same call repeatedly.
Returning arr[n] causes out-of-bounds access.
2fill in blank
medium

Complete the code to check if a string is a palindrome using recursion.

DSA C
int isPalindrome(char str[], int start, int end) {
    if (start >= end) {
        return 1;
    }
    if (str[start] != str[end]) {
        return 0;
    }
    return [1];
}
Drag options to blanks, or click blank then click option'
AisPalindrome(str, start + 1, end - 1)
BisPalindrome(str, start - 1, end + 1)
CisPalindrome(str, start, end)
DisPalindrome(str, end, start)
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping start and end causes wrong checks.
Using same start and end causes infinite recursion.
Decreasing start or increasing end moves away from center.
3fill in blank
hard

Fix the error in the recursive function to count occurrences of a character in a string.

DSA C
int countChar(char str[], int index, char c) {
    if (str[index] == '\0') {
        return 0;
    }
    int count = (str[index] == c) ? 1 : 0;
    return count + [1];
}
Drag options to blanks, or click blank then click option'
AcountChar(str, 0, c)
BcountChar(str, index - 1, c)
CcountChar(str, index, c)
DcountChar(str, index + 1, c)
Attempts:
3 left
💡 Hint
Common Mistakes
Decreasing index causes infinite recursion or invalid access.
Using same index causes infinite recursion.
Resetting index to 0 causes repeated counting.
4fill in blank
hard

Fill both blanks to create a recursive function that reverses a string in place.

DSA C
void reverseString(char str[], int start, int end) {
    if (start >= end) {
        return;
    }
    char temp = str[[1]];
    str[[2]] = str[start];
    str[start] = temp;
    reverseString(str, start + 1, end - 1);
}
Drag options to blanks, or click blank then click option'
Aend
Bstart
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping with wrong indices causes incorrect reversal.
Using same index twice causes no change.
Not moving indices inward causes infinite recursion.
5fill in blank
hard

Fill all three blanks to create a recursive function that builds a frequency map of characters in a string.

DSA C
void buildFreqMap(char str[], int index, int freq[]) {
    if (str[[1]] == '\0') {
        return;
    }
    freq[str[[2]] - 'a']++;
    buildFreqMap(str, [3], freq);
}
Drag options to blanks, or click blank then click option'
Aindex
Cindex + 1
Dindex - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong index causes incorrect frequency counting.
Decreasing index causes infinite recursion.
Not checking for string end causes errors.