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
| // solution for this hackerrank challenge | |
| // https://www.hackerrank.com/challenges/ctci-queue-using-two-stacks | |
| function Queue() { | |
| this.data = []; | |
| this.operations = { | |
| 1: args => this.enqueue(...args), | |
| 2: args => this.dequeue(...args), | |
| 3: args => this.print(...args) |
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 memoized(fn) { | |
| let cache = {}; | |
| return function(key) { | |
| if(typeof cache[key] !== 'undefined') { | |
| return cache[key]; | |
| }else { | |
| let result = fn.apply(null, arguments); | |
| cache[key] = result; | |
| return result; |
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 throttle(fn, delay) { | |
| let lastTime; | |
| return function() { | |
| let now = new Date().getTime(); | |
| if(!lastTime || now - lastTime > delay) { | |
| fn.apply(null, arguments); | |
| lastTime = now; | |
| } |
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
| /* | |
| Given the words in the magazine and the words in the ransom note, print Yes if he can replicate his ransom note exactly using whole words from the magazine; otherwise, print No. | |
| https://www.hackerrank.com/challenges/ctci-ransom-note?h_r=internal-search | |
| */ | |
| function magazineRansom(magazine, note) { | |
| magazine = magazine | |
| .split(' ') | |
| .reduce((map, word) => { | |
| if (!map[word]) { |
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
| /* | |
| Given two strings, and , that may or may not be of the same length, determine the minimum number of character deletions required to make and anagrams. Any characters can be deleted from either of the strings. | |
| https://www.hackerrank.com/challenges/ctci-making-anagrams | |
| */ | |
| function indexChars(map, char) { | |
| if(!map.hasOwnProperty(char)) { | |
| map[char] = 0; | |
| } | |
| map[char]++; |
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
| /* | |
| A script that processes a matrix that represents bodies of water. Any values that equal 0 represent water. | |
| The script outputs a list of bodies of water found, with their respective sizes. For Example: | |
| let water = [ | |
| [0, 2, 1, 0], | |
| [0, 1, 0, 1], | |
| [1, 1, 0, 1], | |
| [0, 1, 0, 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
| // a script that processes arithmetic in reverse polish notation | |
| //https://en.wikipedia.org/wiki/Reverse_Polish_notation | |
| const operate = { | |
| '+': (a,b) => a+b, | |
| '-': (a,b) => a-b, | |
| '*': (a,b) => a*b, | |
| '/': (a,b) => a/b, | |
| }; |
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 flattenObj(obj, keyChain=[], flattened={}) { | |
| for(let key in obj) { | |
| let value = obj[key]; | |
| let keys = keyChain.concat(key); | |
| if(value instanceof Object) { | |
| flattenObj(value, keys, flattened); | |
| }else { | |
| flattened[keys.join('.')] = value; | |
| } |
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
| Array.prototype.swap = function(idx1, idx2) { | |
| if(idx1 >= this.length || idx2 >= this.length) { | |
| throw new Error("index out of bounds"); | |
| } | |
| let first = Math.max(idx1, idx2); | |
| let last = Math.min(idx1, idx2); | |
| let value = this.splice(first, 1, this[last])[0]; |
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
| //Given an array of positive integers and a target total of X, find if there exists a contiguous subarray with sum = X | |
| var list=[1,3,5,18]; | |
| function isValid(arr, target) { | |
| var left=0; | |
| var right=0; | |
| while(true) { | |
| var sum = 0; |