Challenge - 5 Problems
Recursion Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Sum of Array Elements
What is the output of the following Java code that recursively sums elements of an array?
DSA Java
public class Main { public static int recursiveSum(int[] arr, int index) { if (index == arr.length) { return 0; } return arr[index] + recursiveSum(arr, index + 1); } public static void main(String[] args) { int[] numbers = {1, 2, 3, 4}; System.out.println(recursiveSum(numbers, 0)); } }
Attempts:
2 left
💡 Hint
Think about how the function adds each element starting from index 0 until the end.
✗ Incorrect
The function adds each element of the array starting from index 0 recursively until it reaches the end, summing 1+2+3+4 = 10.
❓ Predict Output
intermediateOutput of Recursive String Reverse
What is the output of this Java code that recursively reverses a string?
DSA Java
public class Main { public static String reverseString(String s) { if (s.isEmpty()) { return s; } return reverseString(s.substring(1)) + s.charAt(0); } public static void main(String[] args) { System.out.println(reverseString("code")); } }
Attempts:
2 left
💡 Hint
The function takes the first character and puts it at the end after reversing the rest.
✗ Incorrect
The function reverses the string by recursively calling itself on the substring excluding the first character and then appending the first character at the end.
🧠 Conceptual
advancedUnderstanding Base Case in Recursive Array Search
In a recursive function that searches for a target value in an array, what is the correct base case to stop recursion?
Attempts:
2 left
💡 Hint
Think about when the function should stop searching without finding the target.
✗ Incorrect
The base case is when the index reaches the length of the array, meaning the entire array has been searched without finding the target.
🔧 Debug
advancedIdentify the Error in Recursive Palindrome Check
What error will occur when running this Java code that checks if a string is a palindrome recursively?
DSA Java
public class Main { public static boolean isPalindrome(String s, int start, int end) { if (start >= end) { return true; } if (s.charAt(start) != s.charAt(end)) { return false; } return isPalindrome(s, start + 1, end - 1); } public static void main(String[] args) { System.out.println(isPalindrome("racecar", 0, 6)); } }
Attempts:
2 left
💡 Hint
Check the end index passed to the function and string length.
✗ Incorrect
The end index should be 6 for "racecar" (length 7) because indices go from 0 to 6, so no exception occurs.
🚀 Application
expertResult of Recursive Flattening of Nested Integer Arrays
Given the following Java code that recursively flattens nested integer arrays into a single list, what is the printed output?
DSA Java
import java.util.*; public class Main { public static void flatten(Object[] arr, List<Integer> result) { for (Object obj : arr) { if (obj instanceof Integer) { result.add((Integer) obj); } else if (obj instanceof Object[]) { flatten((Object[]) obj, result); } } } public static void main(String[] args) { Object[] nested = {1, new Object[]{2, 3}, 4, new Object[]{new Object[]{5}, 6}}; List<Integer> flatList = new ArrayList<>(); flatten(nested, flatList); System.out.println(flatList); } }
Attempts:
2 left
💡 Hint
The function adds integers directly and recursively processes nested arrays.
✗ Incorrect
The function recursively visits each element, adding integers to the list and flattening nested arrays, resulting in [1, 2, 3, 4, 5, 6].
