Bird
Raised Fist0
DSA C++programming~20 mins

Recursion on Arrays and Strings in DSA C++ - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Recursion Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Recursive Sum on Array
What is the output of the following C++ code that recursively sums elements of an array?
DSA C++
#include <iostream>

int recursiveSum(int arr[], int n) {
    if (n == 0) return 0;
    return arr[n-1] + recursiveSum(arr, n-1);
}

int main() {
    int arr[] = {1, 2, 3, 4};
    int result = recursiveSum(arr, 4);
    std::cout << result << std::endl;
    return 0;
}
A24
B10
C0
DCompilation error
Attempts:
2 left
💡 Hint
Think about how the function adds the last element and calls itself with one less element.
❓ Predict Output
intermediate
2:00remaining
Output of Recursive String Reverse
What is the output of this C++ code that recursively reverses a string?
DSA C++
#include <iostream>
#include <string>

std::string reverseString(const std::string& s) {
    if (s.empty()) return "";
    return reverseString(s.substr(1)) + s[0];
}

int main() {
    std::string input = "code";
    std::cout << reverseString(input) << std::endl;
    return 0;
}
Aedoc
Bcode
CCompilation error
Dedocd
Attempts:
2 left
💡 Hint
The function calls itself on the substring excluding the first character, then adds the first character at the end.
🧠 Conceptual
advanced
1:30remaining
Recursion Base Case Importance
Why is a base case necessary in a recursive function that processes arrays or strings?
ATo convert recursion into iteration
BTo allocate memory dynamically
CTo stop the recursion and prevent infinite calls
DTo make the function run faster
Attempts:
2 left
💡 Hint
Think about what happens if recursion never stops.
🔧 Debug
advanced
2:00remaining
Identify the Error in Recursive Palindrome Check
What error does this C++ code produce when checking if a string is a palindrome recursively?
DSA C++
#include <iostream>
#include <string>

bool isPalindrome(const std::string& s, int start, int end) {
    if (start >= end) return true;
    if (s[start] != s[end]) return false;
    return isPalindrome(s, start + 1, end - 1);
}

int main() {
    std::string test = "level";
    std::cout << isPalindrome(test, 0, test.size() - 1) << std::endl;
    return 0;
}
AOutputs 1 (true) correctly
BCompilation error due to missing header
CRuntime error due to out-of-range index
DOutputs 0 (false) incorrectly
Attempts:
2 left
💡 Hint
Check the value of 'end' passed to the function and how it is used as an index.
🚀 Application
expert
2:30remaining
Number of Recursive Calls in Fibonacci
How many times is the function called (including the first call) when computing fibonacci(4) using this naive recursive code?
DSA C++
#include <iostream>

int fibonacci(int n) {
    if (n <= 1) return n;
    return fibonacci(n-1) + fibonacci(n-2);
}

int main() {
    int calls = fibonacci(4);
    std::cout << calls << std::endl;
    return 0;
}
A15
B7
C5
D9
Attempts:
2 left
💡 Hint
Draw the call tree for fibonacci(4) and count all calls.

Practice

(1/5)
1. What does a recursive function on an array typically do to process the array elements?
easy
A. It processes one element and calls itself on the rest of the array
B. It processes all elements at once without calling itself
C. It uses loops instead of calling itself
D. It only processes the last element of the array

Solution

  1. Step 1: Understand recursion on arrays

    A recursive function on an array usually handles one element and then calls itself on the remaining elements.
  2. Step 2: Compare options

    It processes one element and calls itself on the rest of the array matches this behavior. Options A, C, and D describe non-recursive or incorrect methods.
  3. Final Answer:

    It processes one element and calls itself on the rest of the array -> Option A
  4. Quick Check:

    Recursion on arrays = process one element + recurse [OK]
Hint: Remember recursion breaks problem into smaller parts [OK]
Common Mistakes:
  • Thinking recursion processes all elements at once
  • Confusing recursion with loops
  • Assuming recursion only processes last element
2. Which of the following is the correct base case for a recursive function that sums elements of an integer array arr of size n?
easy
A. if (n == 0) return 0;
B. if (n == 1) return arr[1];
C. if (arr == NULL) return 0;
D. if (n < 0) return 0;

Solution

  1. Step 1: Identify base case for recursion on array size

    The base case should stop recursion when no elements are left, which is when n == 0.
  2. Step 2: Check each option

    if (n == 0) return 0; returns 0 when n == 0, which is correct. if (n == 1) return arr[1]; incorrectly accesses arr[1] instead of arr[0]. if (arr == NULL) return 0; checks pointer but not size. if (n < 0) return 0; uses invalid negative size.
  3. Final Answer:

    if (n == 0) return 0; -> Option A
  4. Quick Check:

    Base case for empty array = return 0 [OK]
Hint: Base case usually when size is zero [OK]
Common Mistakes:
  • Using wrong index in base case
  • Checking pointer instead of size
  • Allowing negative sizes
3. What is the output of the following C++ code?
int sumArray(int arr[], int n) {
  if (n == 0) return 0;
  return arr[n-1] + sumArray(arr, n-1);
}

int main() {
  int arr[] = {1, 2, 3, 4};
  int result = sumArray(arr, 4);
  std::cout << result;
  return 0;
}
medium
A. Compilation error
B. 24
C. 0
D. 10

Solution

  1. Step 1: Trace recursive calls

    sumArray(arr,4) = arr[3] + sumArray(arr,3) = 4 + sumArray(arr,3) sumArray(arr,3) = arr[2] + sumArray(arr,2) = 3 + sumArray(arr,2) sumArray(arr,2) = arr[1] + sumArray(arr,1) = 2 + sumArray(arr,1) sumArray(arr,1) = arr[0] + sumArray(arr,0) = 1 + 0 = 1
  2. Step 2: Calculate total sum

    Total = 4 + 3 + 2 + 1 = 10
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    Sum of {1,2,3,4} = 10 [OK]
Hint: Sum recursion adds last element + sum of rest [OK]
Common Mistakes:
  • Confusing index n-1 with n
  • Forgetting base case returns 0
  • Assuming sum is product
4. Find the error in this recursive function that counts occurrences of character c in string s:
int countChar(string s, char c, int index) {
  if (index == s.length()) return 0;
  if (s[index] == c) return 1 + countChar(s, c, index++);
  else return countChar(s, c, index++);
}
medium
A. Missing check for empty string
B. Base case condition is wrong
C. Using post-increment (index++) causes infinite recursion
D. Function should return bool, not int

Solution

  1. Step 1: Analyze recursive calls with index++

    Using index++ passes the current index value before increment, so recursive call uses same index repeatedly.
  2. Step 2: Identify infinite recursion cause

    Because index does not increase in recursive calls, base case is never reached, causing infinite recursion.
  3. Final Answer:

    Using post-increment (index++) causes infinite recursion -> Option C
  4. Quick Check:

    Post-increment in recursion argument = infinite loop [OK]
Hint: Use pre-increment (++index) or index+1 in recursion call [OK]
Common Mistakes:
  • Using post-increment in recursive call argument
  • Ignoring base case correctness
  • Confusing return types
5. Given a string s, write a recursive function to check if it is a palindrome (reads same forward and backward). Which approach below correctly implements this?
hard
A. Reverse string using loop and compare with original
B. Check if first and last chars match, then recurse on substring excluding them
C. Count matching characters from start only
D. Use recursion to count characters and compare counts

Solution

  1. Step 1: Understand palindrome check recursively

    To check palindrome recursively, compare first and last characters; if they match, recurse on substring excluding them.
  2. Step 2: Evaluate options

    Check if first and last chars match, then recurse on substring excluding them matches this logic. Reverse string using loop and compare with original uses loops, not recursion. Options C and D do not correctly check palindrome property.
  3. Final Answer:

    Check if first and last chars match, then recurse on substring excluding them -> Option B
  4. Quick Check:

    Palindrome recursion = compare ends + recurse inside [OK]
Hint: Compare ends, recurse inside substring [OK]
Common Mistakes:
  • Using loops instead of recursion
  • Only checking one side of string
  • Counting chars instead of comparing
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