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
/* | |
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 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 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 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 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 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
// https://www.hackerrank.com/challenges/ctci-ice-cream-parlor | |
// find 2 distinct flavors that would take up all of the money m | |
// for each trip print the id numbers of the two types of ice cream | |
// some ice creams may have the same price | |
function purchase(indexed, product) { | |
let result = Object.assign({}, indexed); |
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
// https://www.hackerrank.com/challenges/ctci-contacts | |
function Trie() { | |
this.data = {}; | |
} | |
Trie.prototype.process = function(op, str) { | |
if(op === 'add') { | |
let data = this.data; |
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
// https://www.hackerrank.com/challenges/ctci-find-the-running-median | |
let input = `6 | |
12 | |
4 | |
5 | |
3 | |
8 | |
7`; |
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
function merge(arrA, arrB) { | |
let idxA=0, idxB=0, result=[]; | |
while(idxA < arrA.length || idxB < arrB.length) { | |
if(idxB === arrB.length || arrA[idxA] <= arrB[idxB]) { | |
result.push(arrA[idxA]); | |
idxA++; | |
}else if(idxA === arrA.length || arrB[idxB] <= arrA[idxA]) { | |
result.push(arrB[idxB]); |
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
// A prime number is a natural number that has exactly two distinct natural number divisors: 1 and itself. | |
const isPrime = (function () { | |
let primes = {}; | |
let notPrimes = {}; | |
return function (number) { | |
if(primes[number]) return true; | |
if(number === 1 || notPrimes[number]) return false; |