Bird
Raised Fist0
DSA Cprogramming~10 mins

Recursion on Arrays and Strings in DSA C - Interactive Practice

Choose your learning style10 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.

Practice

(1/5)
1. What does recursion mean when applied to arrays or strings?
easy
A. A function calls itself to solve smaller parts of the problem
B. A loop runs through all elements once
C. The array or string is sorted automatically
D. The data is copied to a new array or string

Solution

  1. Step 1: Understand recursion concept

    Recursion means a function calls itself to break down a problem into smaller parts.
  2. Step 2: Apply to arrays or strings

    For arrays or strings, recursion processes one element and then calls itself on the rest.
  3. Final Answer:

    A function calls itself to solve smaller parts of the problem -> Option A
  4. Quick Check:

    Recursion = function calls itself [OK]
Hint: Recursion always involves a function calling itself [OK]
Common Mistakes:
  • Confusing recursion with loops
  • Thinking recursion sorts data automatically
  • Believing recursion copies data
2. Which of the following is the correct base case for a recursive function that processes a string?
easy
A. if (length < 0) return;
B. if (length == 0) return;
C. if (string == NULL) continue;
D. if (length > 0) return;

Solution

  1. Step 1: Identify base case role

    The base case stops recursion when no more characters remain, usually length 0.
  2. Step 2: Check options for correct base case

    if (length == 0) return; correctly returns when length is 0, stopping recursion safely.
  3. Final Answer:

    if (length == 0) return; -> Option B
  4. Quick Check:

    Base case stops recursion at empty string [OK]
Hint: Base case usually checks for empty string or zero length [OK]
Common Mistakes:
  • Using incorrect base case conditions
  • Using continue instead of return
  • Checking for negative length which is invalid
3. What is the output of this C code?
void printReverse(char *str) {
  if (*str == '\0') return;
  printReverse(str + 1);
  printf("%c", *str);
}

int main() {
  char s[] = "abc";
  printReverse(s);
  return 0;
}
medium
A. cba
B. abc
C. Syntax error
D. No output

Solution

  1. Step 1: Understand recursion flow

    The function calls itself moving forward in the string until it hits '\0' (end).
  2. Step 2: Print characters on return

    After reaching end, it prints characters backward as recursion unwinds: 'c', 'b', 'a'.
  3. Final Answer:

    cba -> Option A
  4. Quick Check:

    Recursion prints string in reverse [OK]
Hint: Recursion with print after call reverses output [OK]
Common Mistakes:
  • Expecting normal order output
  • Confusing base case with print order
  • Thinking code has syntax errors
4. Find the error in this recursive function that counts characters in a string:
int countChars(char *str) {
  if (*str == '\0')
    return 0;
  return 1 + countChars(str);
}
medium
A. Missing base case
B. Return type should be void
C. Recursive call does not move forward in string
D. Should use while loop instead

Solution

  1. Step 1: Check base case correctness

    Base case correctly returns 0 when string ends.
  2. Step 2: Analyze recursive call

    Recursive call uses same pointer 'str' without moving forward, causing infinite recursion.
  3. Final Answer:

    Recursive call does not move forward in string -> Option C
  4. Quick Check:

    Pointer must advance in recursion [OK]
Hint: Always move pointer forward in recursive calls on strings [OK]
Common Mistakes:
  • Forgetting to advance pointer
  • Changing return type unnecessarily
  • Replacing recursion with loops without reason
5. You want to write a recursive function to check if a string is a palindrome (reads same forward and backward). Which approach is best?
hard
A. Count characters recursively and check if count is even
B. Reverse the string using recursion and compare with original
C. Use recursion to sort the string and compare with original
D. Compare first and last characters, then recurse on substring excluding them

Solution

  1. Step 1: Understand palindrome check logic

    Palindrome means first and last characters must match, then check inside substring.
  2. Step 2: Apply recursion on substring

    Recursively check smaller substring by removing first and last characters each time.
  3. Final Answer:

    Compare first and last characters, then recurse on substring excluding them -> Option D
  4. Quick Check:

    Check ends and recurse inside for palindrome [OK]
Hint: Check ends match, then recurse inside substring [OK]
Common Mistakes:
  • Reversing string unnecessarily
  • Counting characters instead of comparing ends
  • Sorting string which changes order
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