Instructions to candidate.
1) Run this code in the REPL to observe its behaviour. The
execution entry point is main().
2) Implement the "put" and "contains" methods.
3) Fix the "inOrderTraversal" method.
4) Add additional relevant tests
5) If time permits, try to improve your implementation.
let _ = require("underscore");
function put(node, value) {
function contains(node, value) {
function inOrderTraversal(node) {
return inOrderTraversalAcc(node, []);
function inOrderTraversalAcc(node, acc) {
inOrderTraversalAcc(node.left, acc);
inOrderTraversalAcc(node.right, acc);
function assert(condition) {
if (!condition) throw new Error();
assert(!contains(tree, 0));
assert(contains(tree, 1));
assert(contains(tree, 2));
assert(contains(tree, 3));
assert(!contains(tree, 4));
assert(contains(tree, 5));
assert(!contains(tree, 6));
assert(_.isEqual(inOrderTraversal(tree), [1, 2, 3, 5]));
let _ = require("underscore");
function put(node, value) {
if (value < node.value) {
function contains(node, value) {
} else if (node.value === value) {
} else if (node.value && value < node.value) {
return contains(node.left, value);
return contains(node.right, value);
function inOrderTraversal(node) {
return inOrderTraversalAcc(node, []);
function inOrderTraversalAcc(node, acc) {
inOrderTraversalAcc(node.left, acc);
inOrderTraversalAcc(node.right, acc);
function assert(condition) {
assert(!contains(tree, 0));
assert(contains(tree, 1));
assert(contains(tree, 2));
assert(contains(tree, 3));
assert(!contains(tree, 4));
assert(contains(tree, 5));
assert(!contains(tree, 6));
assert(_.isEqual(inOrderTraversal(tree), [1, 2, 3, 5]));
function testEmptyTree() {
let emptyTree = new Node();
assert(_.isEqual(inOrderTraversal(emptyTree), []));
function testNegative() {
let negativeTree = new Node();
assert(contains(negativeTree, -10));
assert(contains(negativeTree, -1));
assert(contains(negativeTree, 11));
assert(contains(negativeTree, 50));
assert(_.isEqual(inOrderTraversal(negativeTree), [-10, -1, 11, 50]));
let dupeTree = new Node();
assert(contains(dupeTree, 1));
assert(contains(dupeTree, 2));
assert(_.isEqual(inOrderTraversal(dupeTree), [1, 1, 2, 2]));
function testUndefined() {
let undefinedTree = new Node();
put(undefinedTree, undefined);
assert(!contains(undefinedTree, undefined));
assert(_.isEqual(inOrderTraversal(undefinedTree), []));
let nullTree = new Node();
assert(!contains(nullTree, null));
assert(_.isEqual(inOrderTraversal(nullTree), []));
* Returns the second smallest element in the array x.
* Returns 0 if the array has fewer than 2 elements.
function secondSmallest(x) {
// todo: implement this function
* Returns true if all tests pass; otherwise, returns false.
// todo: add more test cases
let testArrays = [[0], [0, 1]];
let testResults = [0, 1];
// Run through the tests and make assertions
for (let i = 0; i < testArrays.length; i++) {
if (secondSmallest(testArrays[i]) != testResults[i]) {
console.log("All tests pass!");
console.log("There are test failures.");
function secondSmallest(x) {
// First check if the array is large enough
// Start these at infinity so that they're always bigger
let smallest = Number.POSITIVE_INFINITY;
let secondSmallest = Number.POSITIVE_INFINITY;
// Loop through the input and keep updating our
// smallest and second smallest
for (let i = 0; i < x.length; i++) {
if (current < smallest) {
secondSmallest = smallest;
} else if (current < secondSmallest) {
secondSmallest = current;
* Returns true if all tests pass; otherwise, returns false.
// todo: add more test cases
let testArrays = [[], [0], [0, 1], [-1, 0, 1, -2, 2], [1, 1, 2]];
let testResults = [0, 0, 1, -1, 1];
// Run through the tests and make assertions
for (let i = 0; i < testArrays.length; i++) {
if (secondSmallest(testArrays[i]) != testResults[i]) {
console.log("All tests pass!");
console.log("There are test failures.");