Skip to content

Instantly share code, notes, and snippets.

@dliv
Last active December 9, 2015 07:06
Show Gist options
  • Save dliv/a86354b6ba45f73c799a to your computer and use it in GitHub Desktop.
Save dliv/a86354b6ba45f73c799a to your computer and use it in GitHub Desktop.
interview - coding screen
var assert = require('assert');
/********************************
* We want make a package of goal kilos of skittles. We have
* inventory of small bags (1 kilos each) and big bags (5 kilos each).
* Return the number of small bags to use, assuming we always
* use big bags before small bags. Return -1 if it can't be done.
*
* See the asserts below for examples of input
* and expected output.
*
* If you have node installed, all you need to do to test
* your code is run: `node skittles.js`. If you see errors,
* it is because the tests below did not pass. Once the
* tests do pass, you will see a log of `Success!`
*
* YOUR CODE BELOW HERE
********************************/
function createPackage(small, big, goal) {
if((small + (big * 5)) < goal) {
return -1;
}
var bigKilos = big * 5; // should really use this, the code works for tests but isn't right
var needSmall = goal % 5;
return needSmall <= small ? needSmall : -1;
}
/********************************
* YOUR CODE ABOVE HERE
********************************/
assert.equal(
createPackage(4, 1, 9),
4
);
assert.equal(
createPackage(4, 1, 10),
-1
);
assert.equal(
createPackage(4, 1, 7),
2
);
assert.equal(
createPackage(6, 2, 7),
2
);
assert.equal(
createPackage(4, 1, 5),
0
);
assert.equal(
createPackage(4, 1, 4),
4
);
assert.equal(
createPackage(5, 4, 9),
4
);
assert.equal(
createPackage(9, 3, 18),
3
);
console.log('Success!');
var assert = require('assert');
/********************************
* Return true if the array contains, somewhere, three increasing
* adjacent numbers like .... 4, 5, 6, ... or 23, 24, 25.
*
* See the asserts below for examples of input
* and expected output.
*
* If you have node installed, all you need to do to test
* your code is run: `node tripleThreat.js`. If you see errors,
* it is because the tests below did not pass. Once the
* tests do pass, you will see a log of `Success!`
*
* YOUR CODE BELOW HERE
********************************/
function tripleThreat(array) {
var last;
var increaseCount = 0;
array.forEach(function (val) {
if (increaseCount >= 2) { // two increases for three consecutive
return;
}
if (val === last + 1) {
++increaseCount;
} else {
increaseCount = 0;
}
last = val;
});
return !!(increaseCount >= 2); // two increases for three consecutive
}
/********************************
* YOUR CODE ABOVE HERE
********************************/
assert.equal(
tripleThreat([1, 4, 5, 6, 2]),
true
);
assert.equal(
tripleThreat([1, 2, 3]),
true
);
assert.equal(
tripleThreat([1, 2, 4, 5, 7, 6, 5, 6, 7, 6]),
true
);
assert.equal(
tripleThreat([1, 2, 4, 5, 7, 6, 5, 7, 7, 6]),
false
);
assert.equal(
tripleThreat([1,2]),
false
);
assert.equal(
tripleThreat([10, 9, 8, -100, -99, -98, 100]),
true
);
assert.equal(
tripleThreat([10, 9, 8, -100, -99, 99, 100]),
false
);
console.log('Success!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment