Skip to content

Instantly share code, notes, and snippets.

@jasonwaters
jasonwaters / anagram-removals.js
Created January 25, 2017 23:43
anagram removals
/*
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]++;
@jasonwaters
jasonwaters / magazine-ransom.js
Created January 26, 2017 15:50
magazine ransom
/*
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]) {
@jasonwaters
jasonwaters / debounce-throttle.js
Created January 27, 2017 17:13
debounce and throtle
function throttle(fn, delay) {
let lastTime;
return function() {
let now = new Date().getTime();
if(!lastTime || now - lastTime > delay) {
fn.apply(null, arguments);
lastTime = now;
}
@jasonwaters
jasonwaters / memoize.js
Created January 27, 2017 18:06
memoize function
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;
@jasonwaters
jasonwaters / queue.js
Created January 31, 2017 20:54
queue
// 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)
@jasonwaters
jasonwaters / ice-cream-purchase.js
Created February 2, 2017 21:00
Ice Cream Purchases
// 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);
@jasonwaters
jasonwaters / tries.js
Last active February 6, 2017 20:45
Tries: Contacts
// https://www.hackerrank.com/challenges/ctci-contacts
function Trie() {
this.data = {};
}
Trie.prototype.process = function(op, str) {
if(op === 'add') {
let data = this.data;
@jasonwaters
jasonwaters / running-median.js
Created February 6, 2017 22:56
Heaps: Find the Running Median
// https://www.hackerrank.com/challenges/ctci-find-the-running-median
let input = `6
12
4
5
3
8
7`;
@jasonwaters
jasonwaters / merge-sort.js
Created February 7, 2017 22:46
Merge Sort
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]);
@jasonwaters
jasonwaters / is-prime.js
Created February 9, 2017 17:33
is prime?
// 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;