0
0
DSA Typescriptprogramming~20 mins

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

Choose your learning style9 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 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);
ATypeError
B10
C0
D24
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 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);
A"
Bcode
Cedoc
DRangeError
Attempts:
2 left
💡 Hint
The function takes the first character and puts it at the end after reversing the rest.
🔧 Debug
advanced
2: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));
ANo error, outputs true
BTypeError
Cundefined is not a number
DStackOverflowError
Attempts:
2 left
💡 Hint
Check the base case condition for index exceeding array length.
Predict Output
advanced
2: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));
A[1, 2, 3, 4, 5, 6]
B[1, [2, [3, 4], 5], 6]
C[[1, 2], 3, 4, 5, 6]
DTypeError
Attempts:
2 left
💡 Hint
The function checks if the first element is an array and flattens it recursively.
🧠 Conceptual
expert
2: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);
}
An - 1
Bn
CMath.ceil(n / 2)
DMath.floor(n / 2)
Attempts:
2 left
💡 Hint
Each recursive call moves inward from both ends by one character.