Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

Given an array a, check to see if z numbers inside a total up to n.

If a = [1,2,3], n=3 and z=2, then this function should return true, since z numbers in that array combine to equal n.

I recently took a test for a job to fill out this function, and this was my answer. It was presumably rejected by the robots for being too slow, since one of the tests terminated with a timeout error.

var isSumPossible = function isSumPossible(a, n, z) {
 return +a.some(function (val, ix) {
 return z === 1 ? val === n : isSumPossible(a.slice(ix - 1, ix).concat(a.slice(ix + 1)), n - val, z - 1);
 });
};

How can this code be optimized to make it faster?

Given an array a, check to see if z numbers inside a total up to n.

If a = [1,2,3], n=3 and z=2, then this function should return true, since z numbers in that array combine to equal n.

I recently took a test for a job to fill out this function, and this was my answer. It was presumably rejected by the robots for being too slow, since one of the tests terminated with a timeout error.

var isSumPossible = function isSumPossible(a, n, z) {
 return +a.some(function (val, ix) {
 return z === 1 ? val === n : isSumPossible(a.slice(ix - 1, ix).concat(a.slice(ix + 1)), n - val, z - 1);
 });
};

How can this code be optimized to make it faster?

Given an array a, check to see if z numbers inside a total up to n.

If a = [1,2,3], n=3 and z=2, then this function should return true, since z numbers in that array combine to equal n.

I recently took a test for a job to fill out this function, and this was my answer. It was presumably rejected by the robots for being too slow, since one of the tests terminated with a timeout error.

var isSumPossible = function isSumPossible(a, n, z) {
 return +a.some(function (val, ix) {
 return z === 1 ? val === n : isSumPossible(a.slice(ix - 1, ix).concat(a.slice(ix + 1)), n - val, z - 1);
 });
};

How can this code be optimized to make it faster?

added 3 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Given an array a, check to see if z numbers inside a total up to n

Given an array a, check to see if z numbers inside a total up to n.

So if a = [1,2,3], n=3 and z=2, then this function should return true, since z numbers in that array combine to equal n.

If a = [1,2,3], n=3 and z=2, then this function should return true, since z numbers in that array combine to equal n.

I recently took a test for a job to fill out this function, and this was my answer. It was presumably rejected by the robots for being too slow, since one of the tests terminated with a timeout error.

var isSumPossible = function isSumPossible(a, n, z) {
 return +a.some(function (val, ix) {
 return z === 1 ? val === n : isSumPossible(a.slice(ix - 1, ix).concat(a.slice(ix + 1)), n - val, z - 1);
 });
};

How can this code be optimized to make it faster?

Given an array a, check to see if z numbers inside a total up to n

So if a = [1,2,3], n=3 and z=2, then this function should return true, since z numbers in that array combine to equal n.

I recently took a test for a job to fill out this function, and this was my answer. It was presumably rejected by the robots for being too slow, since one of the tests terminated with a timeout error

var isSumPossible = function isSumPossible(a, n, z) {
 return +a.some(function (val, ix) {
 return z === 1 ? val === n : isSumPossible(a.slice(ix - 1, ix).concat(a.slice(ix + 1)), n - val, z - 1);
 });
};

How can this code be optimized to make it faster?

Given an array a, check to see if z numbers inside a total up to n.

If a = [1,2,3], n=3 and z=2, then this function should return true, since z numbers in that array combine to equal n.

I recently took a test for a job to fill out this function, and this was my answer. It was presumably rejected by the robots for being too slow, since one of the tests terminated with a timeout error.

var isSumPossible = function isSumPossible(a, n, z) {
 return +a.some(function (val, ix) {
 return z === 1 ? val === n : isSumPossible(a.slice(ix - 1, ix).concat(a.slice(ix + 1)), n - val, z - 1);
 });
};

How can this code be optimized to make it faster?

edited tags
Link
200_success
  • 145.6k
  • 22
  • 190
  • 479
Source Link
user33799
user33799
Loading
default

AltStyle によって変換されたページ (->オリジナル) /