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 does the following function do?

View

What will deepSum({a: 1, b: {c: 2, d: {e: 3}}}) return?

View

What will countKeys({a: 1, b: {c: 2, d: {e: 3}}}) return?

View

Which of the following is true about recursive object traversal?

View

What does the following function do?

View

What is the time complexity of recursively traversing a deeply nested object?

View

How can recursion be used to modify deeply nested values in an object?

View

What are the key differences between recursive functions that operate on arrays versus those that operate on objects?

View

Why is recursion particularly useful when working with deeply nested objects?

View

What potential risks should you be aware of when using recursion to traverse deeply nested objects?

View

What does the following function do?

function findValue(obj, key) { if (obj.hasOwnProperty(key)) { return obj[key]; } for (let k in obj) { if (typeof obj[k] === "object") { let result = findValue(obj[k], key); if (result) return result; } } return null; }

It finds a key in the object and returns its value
It returns null for all inputs
It creates a new object
It checks if the object has nested properties

What will deepSum({a: 1, b: {c: 2, d: {e: 3}}}) return?

function deepSum(obj) { let sum = 0; for (let key in obj) { if (typeof obj[key] === "number") { sum += obj[key]; } else if (typeof obj[key] === "object") { sum += deepSum(obj[key]); } } return sum; }

6
1
5
0

What will countKeys({a: 1, b: {c: 2, d: {e: 3}}}) return?

function countKeys(obj) { let count = 0; for (let key in obj) { count++; if (typeof obj[key] === "object") { count += countKeys(obj[key]); } } return count; }

5
4
3
2

Which of the following is true about recursive object traversal?

It’s necessary to flatten objects before recursion
Recursion is useful when objects are deeply nested
Object traversal is only possible with loops, not recursion
Recursion only works with arrays, not objects

What does the following function do?

function deepCopy(obj) { let newObj = Array.isArray(obj) ? [] : {}; for (let key in obj) { if (typeof obj[key] === "object") { newObj[key] = deepCopy(obj[key]); } else { newObj[key] = obj[key]; } } return newObj; }

It performs a deep copy of the object or array
It performs a shallow copy of the object
It mutates the original object
It flattens the object

What is the time complexity of recursively traversing a deeply nested object?

O(n)
O(n^2)
O(n!)
O(log n)

How can recursion be used to modify deeply nested values in an object?

By traversing the object and checking for values
By making shallow copies
By accessing the first level of the object only
Recursion doesn’t work with objects

What are the key differences between recursive functions that operate on arrays versus those that operate on objects?

Recursive functions for arrays always need a base case, but those for objects do not.
Recursion on arrays often involves slicing or modifying indices, while recursion on objects typically involves iterating over keys and handling nested properties.
Recursive functions on arrays are inherently faster than those on objects.
Object recursion cannot handle deeply nested structures as well as array recursion.

Why is recursion particularly useful when working with deeply nested objects?

Loops are generally slower and less readable than recursion.
JavaScript doesn’t support iteration over objects, so recursion is the only option.
Recursion allows you to handle arbitrarily deep structures without knowing the depth in advance, making it easier to process complex, nested objects.
Recursion always results in faster performance when dealing with large data sets.

What potential risks should you be aware of when using recursion to traverse deeply nested objects?

Recursive functions can lead to performance issues, including stack overflow, if the nesting is too deep, as JavaScript has a limit on the call stack size.
Recursive functions are not compatible with JavaScript objects and should be avoided.
Nested objects are usually too simple to require recursion.
JavaScript automatically optimizes recursive functions on objects, so there are no risks.