Instructions

  • 1. Your final score will reflect your grasp of the concepts—approach each question with precision.
  • 2. Thoroughly review each solution before proceeding to ensure full understanding.
  • 3. Final results will be available after submission to provide insights into areas for further improvement.
  • 4. Maintain academic integrity—plagiarism undermines learning and professional growth.
  • 5. Once submitted, responses are final, so ensure you’re confident in your answers.
  • 6. These challenges are designed to test practical knowledge; apply your skills as you would in real-world scenarios.

All Problems

Question

Action

What will this recursive function return?

View

Which base case will correctly terminate this recursive function for finding the maximum element in an array?

View

What will this function return when called with flatten([1, [2, 3], [4, [5]]])?

View

What does the following recursive function do?

View

What is the time complexity of the following function for summing an array?

View

What does the following code output for findMin([5, 3, 9, 1, 4])?

View

Which of the following is true for this recursive function?

View

What is the role of the slice method in recursive array algorithms?

View

What will this code return for arrayProduct([2, 3, 4])?

View

What does the following recursive function return?

View

What will this recursive function return?

function sumArray(arr) { if (arr.length === 0) { return 0; } else { return arr[0] + sumArray(arr.slice(1)); } } sumArray([1, 2, 3, 4]);

10
6
0
24

Which base case will correctly terminate this recursive function for finding the maximum element in an array?

function findMax(arr) { if (arr.length === 1) { return arr[0]; } // recursive step... }

arr.length === 0
arr[0] === arr[arr.length - 1]
arr.length === 1
arr[0] > arr[1]

What will this function return when called with flatten([1, [2, 3], [4, [5]]])?

function flatten(arr) { if (!Array.isArray(arr)) { return [arr]; } return arr.reduce((acc, curr) => acc.concat(flatten(curr)), []); }

[1, 2, 3, 4, [5]]
[1, 2, 3, 4, 5]
[1, [2, 3], [4, 5]]
[1, [2, 3], 4, [5]]

What does the following recursive function do?

function countDown(n) { if (n === 0) { return []; } else { return [n].concat(countDown(n - 1)); } }

Returns a sum of numbers from n to 0
Returns an array counting down from n to 1
Returns an array counting up from 0 to n
Returns an array counting down from n to 0

What is the time complexity of the following function for summing an array?

function sumArray(arr) { if (arr.length === 0) { return 0; } else { return arr[0] + sumArray(arr.slice(1)); } }

O(1)
O(n)
O(n^2)
O(log n)

What does the following code output for findMin([5, 3, 9, 1, 4])?

function findMin(arr) { if (arr.length === 1) { return arr[0]; } let restMin = findMin(arr.slice(1)); return arr[0] < restMin ? arr[0] : restMin; }

5
9
1
3

Which of the following is true for this recursive function?

function allEven(arr) { if (arr.length === 0) { return true; } return arr[0] % 2 === 0 && allEven(arr.slice(1)); }

It checks if all elements in the array are odd
It checks if all elements in the array are even
It returns true if there's at least one even number
It returns false if all elements are even

What is the role of the slice method in recursive array algorithms?

It reverses the array
It creates a new array without mutating the original array
It combines two arrays
It sorts the array

What will this code return for arrayProduct([2, 3, 4])?

function arrayProduct(arr) { if (arr.length === 0) { return 1; } else { return arr[0] * arrayProduct(arr.slice(1)); } }

0
24
12
6

What does the following recursive function return?

function isPalindrome(str) { if (str.length <= 1) { return true; } if (str[0] !== str[str.length - 1]) { return false; } return isPalindrome(str.slice(1, -1)); }

true if the string is a palindrome, otherwise false
false for all input
It checks if the string contains numbers
It reverses the string