-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
913bc2d
commit 1a82fbb
Showing
3 changed files
with
256 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Prompt | ||
/* | ||
Add a method to BinaryTree called "locateMaxValue". Given the below class, the method should find the max value in the current tree. | ||
Add a method to BinaryTree called "locateMinValue". Given the below class, the method should find the min value in the current tree. | ||
Add a method to BinaryTree called "accumulateTree". This method should return the tree as an array in ANY order. Essentially converting the tree to an array and disregarding any semblance of order. | ||
*/ | ||
|
||
class BinaryTree { | ||
value = null; | ||
|
||
left = null; | ||
|
||
right = null; | ||
|
||
constructor(value) { | ||
this.value = value; | ||
} | ||
|
||
insert = (value) => { | ||
if (value < this.value) { | ||
if (!this.left) { | ||
const createdNode = new BinaryTree(value); | ||
this.left = createdNode; | ||
|
||
return createdNode; | ||
} else { | ||
return this.left.insert(value); | ||
} | ||
} else { | ||
if (!this.right) { | ||
const createdNode = new BinaryTree(value); | ||
this.right = createdNode; | ||
|
||
return createdNode; | ||
} else { | ||
return this.right.insert(value); | ||
} | ||
} | ||
} | ||
|
||
retrieve = (value) => { | ||
if (this.value === value) { | ||
return this.value; | ||
} else if (value < this.value) { | ||
if (!this.left) { | ||
return null; | ||
} else { | ||
return this.left.retrieve(value); | ||
} | ||
} else { | ||
if (!this.right) { | ||
return null; | ||
} else { | ||
return this.right.retrieve(value); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Solution | ||
// I'm writing this as standalone functions to save space. These should actually be written as methods on the class. | ||
|
||
function locateMaxValue() { | ||
if (this.right) { | ||
return this.right.locateMaxValue(); | ||
} else { | ||
return this.value; | ||
} | ||
} | ||
|
||
function locateMinValue() { | ||
if (this.left) { | ||
return this.left.locateMinValue(); | ||
} else { | ||
return this.value; | ||
} | ||
} | ||
|
||
const values = []; | ||
|
||
function accumulateTree(child) { | ||
if (!child) { | ||
return; | ||
} | ||
|
||
values.push(child.value); | ||
|
||
accumulateTree(child.left); | ||
accumulateTree(child.right); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Prompt | ||
// Write a function "hasDuplicates" that uses a hash table to detect if it has seen a value before. The hash table should operate in O(1) time. The complexity of the overall problem should be O(n). | ||
|
||
const hasDuplicates = (nums) => { | ||
// TODO | ||
} | ||
|
||
console.log(hasDuplicates([1, 2, 3, 4, 5])); // => false | ||
console.log(hasDuplicates([6, 7, 8, 9, 6])); // => true | ||
|
||
// Solution | ||
// DO NOT PASTE TO INTERVIEWEE | ||
|
||
const hasDuplicatesSolution = (nums) => { | ||
// Create Hash Table | ||
const collection = {}; | ||
|
||
// Don't get caught up on this - a standard for loop is fine as well. Whatever their flavor for iteration is works! | ||
for (let num of nums) { | ||
// Check the hash table for a collision. | ||
if (collection[num]) { | ||
// If there is a collision, return true. | ||
return true; | ||
} | ||
|
||
// Otherwise, add it to the hash table. | ||
collection[num] = true; | ||
} | ||
|
||
// If no collision occurred, there are no duplicates. | ||
return false; | ||
} | ||
|
||
// Stretch Goal (If Interviewee Gets It) | ||
// Lets take that same function, and allow it to work across function calls. So if I call it with (1, 2, 3) in one call, and (3, 4, 5) in another call, it will correctly identify the second call (3, 4, 5) as having a duplicate (3) because 3 was in both the first arguments (1, 2, 3) and the second (3, 4, 5). | ||
const cacheHasSeenDuplicates = (nums) => { | ||
// TODO | ||
} | ||
|
||
console.log(cacheHasSeenDuplicates([1, 2, 3])); // => false | ||
console.log(cacheHasSeenDuplicates([3, 4, 5])); // => true | ||
|
||
// Stretch Goal Solution | ||
// DO NOT PASTE TO INTERVIEWEE | ||
// Almost the exact same, but you must dig deep in your javascript well to remember closure, which gives functions their own memory store. | ||
const createCacheHasSeenDuplicates = () => { | ||
// Create the hash table in the upper scope (the closure). So that it is accessible to the returned function in perpetuity. | ||
const duplicatesTable = {}; | ||
|
||
return (nums) => { | ||
for (let num of nums) { | ||
// Check the hash table for a collision. | ||
if (duplicatesTable[num]) { | ||
// If there is a collision, return true. | ||
return true; | ||
} | ||
|
||
// Otherwise, add it to the hash table. | ||
duplicatesTable[num] = true; | ||
} | ||
|
||
// If no collision occurred, there are no duplicates. | ||
return false; | ||
}; | ||
} | ||
|
||
const cacheHasSeenDuplicatesSolution = createCacheHasSeenDuplicates(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Trees | ||
|
||
// Guess what Im number Im thinking? | ||
// The number is between 0 and 100 | ||
|
||
// 10 | ||
// 50 | ||
// 76 | ||
// 60 | ||
// 68 | ||
// 64 | ||
// 61 | ||
// 62 | ||
|
||
// They tell you lower or higher at every interval allowing super fast search lookups for particular data | ||
|
||
// 30 | ||
|
||
// 50 | ||
// 25 | ||
// 37 | ||
// 31 | ||
// 28 | ||
// 29 | ||
// 30 | ||
|
||
class BinaryTree { | ||
value = null; | ||
|
||
left = null; | ||
|
||
right = null; | ||
|
||
constructor(value) { | ||
this.value = value; | ||
} | ||
|
||
insert = (value) => { | ||
if (value < this.value) { | ||
if (!this.left) { | ||
const createdNode = new BinaryTree(value); | ||
this.left = createdNode; | ||
|
||
return createdNode; | ||
} else { | ||
return this.left.insert(value); | ||
} | ||
} else { | ||
if (!this.right) { | ||
const createdNode = new BinaryTree(value); | ||
this.right = createdNode; | ||
|
||
return createdNode; | ||
} else { | ||
return this.right.insert(value); | ||
} | ||
} | ||
} | ||
|
||
retrieve = (value) => { | ||
console.log('Searching Into Node with Value: ', this.value); | ||
|
||
if (this.value === value) { | ||
return this.value; | ||
} else if (value < this.value) { | ||
if (!this.left) { | ||
return null; | ||
} else { | ||
return this.left.retrieve(value); | ||
} | ||
} else { | ||
if (!this.right) { | ||
return null; | ||
} else { | ||
return this.right.retrieve(value); | ||
} | ||
} | ||
} | ||
} | ||
|
||
const generateNumbers = (size) => { | ||
const uniqueNumbers = new Set( | ||
new Array(size).fill('').map(() => { | ||
return Math.round(Math.random() * 100000); | ||
}), | ||
); | ||
|
||
return Array.from(uniqueNumbers); | ||
}; | ||
|
||
const allNums = generateNumbers(1000000); | ||
|
||
const root = new BinaryTree(allNums.shift()); | ||
|
||
allNums.forEach((num) => root.insert(num)); | ||
|
||
root.retrieve(321); |