This file contains hidden or 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
| // Iterative function to implement Binary Search | |
| let iterativeFunction = function (arr, x) { | |
| let start=0, end=arr.length-1; | |
| // Iterate while start not meets end | |
| while (start<=end){ | |
| // Find the mid index |
This file contains hidden or 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
| function bfs(tree, value) { | |
| var queue = [] | |
| queue.push(tree[0]) | |
| while (queue.length !== 0) { | |
| for (let i = 0; i < queue.length; i++) { | |
| var node = queue.shift() |
This file contains hidden or 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
| // round robin | |
| // takes arr and index | |
| // iterates through | |
| // good for load balancing | |
| function roundRobin (array, index) { | |
| index = index || 0; | |
| if (array === undefined || array === null) | |
| array = []; | |
| else if (!Array.isArray(array)) |
This file contains hidden or 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
| // LRU Cache | |
| class LruCache { | |
| constructor(capacity) { | |
| this.m = new Map(); | |
| this.capacity = capacity; | |
| } | |
| get(key) { | |
| if (this.m.has(key)) { | |
| const value = this.m.get(key); |
This file contains hidden or 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
| // Design a class to load freight. Freight contains weight and timestamp. | |
| // Add method return total weight. | |
| // Add method return weight at a given time. Probably means to implement some logging system. | |
| class LoadFreight { | |
| constructor () { | |
| this.load = [{ weight: 1230, timeStamp: 'Thu Apr 11 2019' }]; | |
| } |
This file contains hidden or 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
| function weightedSort(data, productLimit) { | |
| var products = []; | |
| var output = []; | |
| var dataSum = 0; | |
| var sum = 0; | |
| var sortedData; | |
| var randomNumber; | |
| var numberToPop; | |
| var noOfproducts = productLimit; | |
| var j; |
This file contains hidden or 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
| // 1, 1, 2, 3, 5, 8 | |
| function fib(n){ | |
| if(n < 2) return n; | |
| return fib(n - 1) + fib(n - 2); | |
| } |
This file contains hidden or 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
| class BinarySearchTree { | |
| constructor(){ | |
| this.root = null; | |
| } | |
| // add a node to the tree | |
| add(value){ | |
| let newNode = { value, left: null, right: null}; | |
This file contains hidden or 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
| function binarySearch(arr, target) { | |
| let left = 0; | |
| let right = arr.length - 1; | |
| while (left <= right) { | |
| const mid = left + Math.floor((right - left) / 2); | |
| if (arr[mid] === target) { | |
| return mid; | |
| } | |
| if (arr[mid] < target) { | |
| left = mid + 1; |
This file contains hidden or 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
| function linearSearch(array, toFind){ | |
| for(let i = 0; i < array.length; i++){ | |
| if(array[i] === toFind) return i; | |
| } | |
| return -1; | |
| } |
NewerOlder