Challenge - 5 Problems
Recursion Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Recursive Sum of Array Elements
What is the output of this TypeScript code that sums elements of an array recursively?
DSA Typescript
function recursiveSum(arr: number[], index: number = 0): number { if (index === arr.length) return 0; return arr[index] + recursiveSum(arr, index + 1); } const result = recursiveSum([1, 2, 3, 4]); console.log(result);
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 starting from index 0 recursively until it reaches the end, summing 1+2+3+4 = 10.
❓ Predict Output
intermediate2:00remaining
Output of Recursive String Reverse
What is the output of this TypeScript code that reverses a string recursively?
DSA Typescript
function reverseString(str: string): string { if (str === "") return ""; return reverseString(str.substr(1)) + str.charAt(0); } const reversed = reverseString("code"); console.log(reversed);
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 appending the first character at the end.
🔧 Debug
advanced2:00remaining
Identify the Error in Recursive Array Search
What error does this TypeScript code produce when searching for a value recursively in an array?
DSA Typescript
function recursiveSearch(arr: number[], target: number, index: number = 0): boolean { if (index >= arr.length) return false; if (arr[index] === target) return true; return recursiveSearch(arr, target, index + 1); } console.log(recursiveSearch([5, 3, 7], 7));
Attempts:
2 left
💡 Hint
Check the base case condition for index exceeding array length.
✗ Incorrect
The base case was originally 'index > arr.length' which allowed index to equal arr.length causing arr[index] to be undefined and comparing undefined to target causes a TypeError. Changing it to 'index >= arr.length' fixes the error.
❓ Predict Output
advanced2:00remaining
Output of Recursive Flattening of Nested Arrays
What is the output of this TypeScript code that recursively flattens a nested array?
DSA Typescript
function flatten(arr: any[]): any[] {
if (arr.length === 0) return [];
const [first, ...rest] = arr;
if (Array.isArray(first)) {
return [...flatten(first), ...flatten(rest)];
} else {
return [first, ...flatten(rest)];
}
}
const nested = [1, [2, [3, 4], 5], 6];
console.log(flatten(nested));Attempts:
2 left
💡 Hint
The function checks if the first element is an array and flattens it recursively.
✗ Incorrect
The function recursively flattens nested arrays by concatenating flattened first element and flattened rest of the array.
🧠 Conceptual
expert2:00remaining
Maximum Call Stack Depth in Recursive Palindrome Check
Consider this recursive TypeScript function that checks if a string is a palindrome. What is the maximum depth of the call stack for a string of length n?
DSA Typescript
function isPalindrome(str: string, left: number = 0, right: number = str.length - 1): boolean { if (left >= right) return true; if (str[left] !== str[right]) return false; return isPalindrome(str, left + 1, right - 1); }
Attempts:
2 left
💡 Hint
Each recursive call moves inward from both ends by one character.
✗ Incorrect
The function calls itself reducing the problem size by two characters each time, so maximum depth is floor of n/2.