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 of Array Elements
What is the output of the following C# code that recursively sums elements of an array?
DSA C#
using System;

class Program {
    static int RecursiveSum(int[] arr, int index) {
        if (index == arr.Length) return 0;
        return arr[index] + RecursiveSum(arr, index + 1);
    }

    static void Main() {
        int[] numbers = {1, 2, 3, 4};
        Console.WriteLine(RecursiveSum(numbers, 0));
    }
}
A0
B24
C10
DThrows an exception
Attempts:
2 left
💡 Hint
Think about how the function adds each element starting from index 0 until the end.
❓ Predict Output
intermediate
2:00remaining
Output of Recursive String Reverse
What is the output of this C# code that reverses a string recursively?
DSA C#
using System;

class Program {
    static string ReverseString(string s) {
        if (s.Length <= 1) return s;
        return ReverseString(s.Substring(1)) + s[0];
    }

    static void Main() {
        string input = "code";
        Console.WriteLine(ReverseString(input));
    }
}
Aedoc
Bcode
CThrows an exception
Dedoced
Attempts:
2 left
💡 Hint
The function takes the first character and puts it at the end after reversing the rest.
❓ Predict Output
advanced
2:00remaining
Output of Recursive Palindrome Check
What is the output of this C# code that checks if a string is a palindrome using recursion?
DSA C#
using System;

class Program {
    static bool IsPalindrome(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);
    }

    static void Main() {
        string test = "racecar";
        Console.WriteLine(IsPalindrome(test, 0, test.Length - 1));
    }
}
ATrue
BFalse
CThrows an exception
Dracecar
Attempts:
2 left
💡 Hint
A palindrome reads the same forwards and backwards.
❓ Predict Output
advanced
2:00remaining
Output of Recursive Fibonacci with Memoization
What is the output of this C# code that calculates the 5th Fibonacci number using recursion with memoization?
DSA C#
using System;
using System.Collections.Generic;

class Program {
    static Dictionary<int, int> memo = new Dictionary<int, int>();

    static int Fibonacci(int n) {
        if (n <= 1) return n;
        if (memo.ContainsKey(n)) return memo[n];
        memo[n] = Fibonacci(n - 1) + Fibonacci(n - 2);
        return memo[n];
    }

    static void Main() {
        Console.WriteLine(Fibonacci(5));
    }
}
A3
B8
CThrows an exception
D5
Attempts:
2 left
💡 Hint
Remember Fibonacci sequence starts 0,1,1,2,3,5,...
🧠 Conceptual
expert
2:00remaining
Maximum Call Stack Depth in Recursive String Palindrome Check
Consider a recursive function that checks if a string of length n is a palindrome by comparing characters from the ends moving inward. What is the maximum depth of the call stack during execution?
An
Bn/2
Clog n
D1
Attempts:
2 left
💡 Hint
Each recursive call moves inward by one character from both ends.

Practice

(1/5)
1. What is the main idea behind using recursion on arrays or strings?
easy
A. Sorting the array or string before processing
B. Using loops to iterate over each element
C. Breaking the problem into smaller similar problems until a base case is reached
D. Storing all elements in a new data structure

Solution

  1. Step 1: Understand recursion concept

    Recursion solves problems by calling itself with smaller inputs until a simple base case is reached.
  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:

    Breaking the problem into smaller similar problems until a base case is reached -> Option C
  4. Quick Check:

    Recursion = divide and conquer [OK]
Hint: Think: solve small part, then repeat on smaller input [OK]
Common Mistakes:
  • Confusing recursion with loops
  • Ignoring the base case
  • Trying to process all elements at once
2. Which of the following is the correct base case for a recursive function that processes a string character by character?
easy
A. if (str[0] == '\0') return;
B. if (str == null) return;
C. if (str.Length > 0) return;
D. if (str.Length == 0) return;

Solution

  1. Step 1: Identify base case for string recursion

    The base case should stop recursion when the string is empty (length zero).
  2. Step 2: Check options

    if (str.Length == 0) return; correctly checks if string length is zero to stop recursion.
  3. Final Answer:

    if (str.Length == 0) return; -> Option D
  4. Quick Check:

    Base case = empty string check [OK]
Hint: Base case usually checks for empty string or array length zero [OK]
Common Mistakes:
  • Checking for null instead of empty string
  • Using wrong condition like length > 0
  • Checking for null character '\0' in C# strings
3. What will be the output of this C# recursive function call?
static int SumArray(int[] arr, int index) {
  if (index == arr.Length) return 0;
  return arr[index] + SumArray(arr, index + 1);
}

int[] nums = {1, 2, 3};
Console.WriteLine(SumArray(nums, 0));
medium
A. 6
B. 0
C. 3
D. StackOverflowException

Solution

  1. Step 1: Trace recursive calls

    SumArray called with index 0 returns arr[0] + SumArray(1) = 1 + ... SumArray(1) returns 2 + SumArray(2) SumArray(2) returns 3 + SumArray(3) SumArray(3) returns 0 (base case)
  2. Step 2: Calculate total sum

    Sum = 1 + 2 + 3 + 0 = 6
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    Sum of [1,2,3] = 6 [OK]
Hint: Add current element + recursive sum of rest [OK]
Common Mistakes:
  • Forgetting base case leads to infinite recursion
  • Returning wrong index or value
  • Confusing index with length
4. Identify the error in this recursive function that reverses a string:
static string Reverse(string s) {
  if (s.Length == 0) return s;
  return Reverse(s.Substring(1)) + s[0];
}

// Usage: Console.WriteLine(Reverse(null));
medium
A. It causes a NullReferenceException when input is null
B. It never reaches the base case
C. It returns the original string without reversing
D. It causes infinite recursion

Solution

  1. Step 1: Analyze input null handling

    The function calls s.Length but if s is null, this causes NullReferenceException.
  2. Step 2: Check base case and usage

    Base case is correct for empty string but does not handle null input, causing runtime error.
  3. Final Answer:

    It causes a NullReferenceException when input is null -> Option A
  4. Quick Check:

    Null input causes exception [OK]
Hint: Always check for null before accessing string properties [OK]
Common Mistakes:
  • Not checking for null input
  • Assuming empty string and null are same
  • Ignoring exceptions from null dereference
5. Given a string, write a recursive function to check if it is a palindrome (reads same forward and backward). Which of these approaches is correct?
hard
A. Use recursion to sort the string and compare with original
B. Check if first and last characters are equal, then recurse on substring excluding them
C. Count characters recursively and check if count is even
D. Reverse the string using recursion and compare with original

Solution

  1. Step 1: Understand palindrome check logic

    Palindrome means first and last characters match, and inner substring is also palindrome.
  2. Step 2: Apply recursion

    Recursively check first and last chars, then call function on substring excluding them until base case (empty or single char).
  3. Final Answer:

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

    Palindrome check = compare ends + recurse [OK]
Hint: Compare ends, then recurse inside substring [OK]
Common Mistakes:
  • Reversing string is less efficient
  • Counting characters doesn't prove palindrome
  • Sorting changes string 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