Bird
Raised Fist0
DSA C#programming~5 mins

Recursion on Arrays and Strings in DSA C# - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is recursion in the context of arrays and strings?
Recursion is a method where a function calls itself to solve smaller parts of a problem, like processing one element of an array or one character of a string at a time until a base case is reached.
Click to reveal answer
beginner
What is a base case in recursion?
A base case is the simplest situation in a recursive function that can be answered directly without further recursion, stopping the recursive calls.
Click to reveal answer
intermediate
How does recursion help in reversing a string?
Recursion reverses a string by taking the first character, calling itself on the rest of the string, and then adding the first character at the end of the reversed substring.
Click to reveal answer
intermediate
Explain how recursion can be used to find the sum of elements in an array.
Recursion sums array elements by adding the first element to the sum of the rest of the array, which is found by calling the same function on the smaller array until the base case (empty array) is reached.
Click to reveal answer
beginner
What happens if a recursive function does not have a proper base case?
Without a proper base case, the recursive function will keep calling itself forever, causing a stack overflow error and crashing the program.
Click to reveal answer
What is the first step in a recursive function processing an array?
AProcess the first element and call recursion on the rest
BProcess the last element only
CStop immediately without processing
DProcess all elements at once
What is the base case when recursively reversing a string?
AWhen the string has two characters
BWhen the string is empty or has one character
CWhen the string is longer than 5 characters
DWhen the string contains only vowels
Which of these is NOT a correct base case for recursion on arrays?
AArray length is greater than zero
BArray length is one
CArray length is negative
DArray length is zero
What will happen if a recursive function lacks a base case?
AIt will return immediately
BIt will skip recursion
CIt will run forever until a crash
DIt will optimize itself
How does recursion simplify problems on strings and arrays?
ABy sorting the data first
BBy using loops instead
CBy ignoring parts of the data
DBy breaking the problem into smaller similar problems
Describe how you would use recursion to find the maximum element in an array.
Think about comparing one element at a time and reducing the problem size.
You got /3 concepts.
    Explain the role of the base case and recursive case in a function that reverses a string recursively.
    Focus on when recursion stops and how the string is rebuilt.
    You got /3 concepts.

      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