Katkat

Implement JavaScript utility and API functions

SapSoftware Engineer · Technical Screen

Last updated

This problem tests proficiency in JavaScript functional programming patterns, data manipulation, and asynchronous error handling when interacting with REST APIs.

Part 1: Functional Utilities

Implement a function curry(fn) that transforms a function of N arguments into a sequence of functions that each take a single argument.

Example:

const sum = (a, b, c) => a + b + c;
const curriedSum = curry(sum);
console.log(curriedSum(1)(2)(3)); // 6

Part 2: Data Transformation

Write a function groupByCategory(items) that takes an array of objects, where each object has a category and value property, and returns an object mapping categories to the sum of their values.

Example:

const data = [{cat: 'A', val: 10}, {cat: 'B', val: 5}, {cat: 'A', val: 20}];
// Output: { 'A': 30, 'B': 5 }

Part 3: API Integration

Write an async function fetchData(url, retries) that performs a GET request using the fetch API. If the request fails or returns a non-200 status code, retry the request up to the specified number of times before throwing an error.

Constraints:

  • Use standard JavaScript (ES6+).
  • Assume fetch is available in the environment.
  • Handle empty arrays and non-numeric values gracefully in Part 2.
  • For Part 3, implement a basic exponential backoff or simple delay if necessary.

Hints

Hint 1

For currying, use recursion to collect arguments in an array until the number of gathered arguments matches the original function's length.

Hint 2

For grouping, use the `Array.prototype.reduce` method to build the results object iteratively.

Hint 3

For the API call, wrap the fetch in a loop or recursive function that checks the response status before resolving the promise.

Follow-up questions

  • How would you modify the curry function to handle an unknown number of arguments instead of a fixed length?
  • What are the performance implications of deep-copying objects when processing large datasets?
  • How would you test the `fetchData` function without making actual network requests to a real server?