Skip to content

Instantly share code, notes, and snippets.

View primaryobjects's full-sized avatar

Kory Becker primaryobjects

View GitHub Profile
@primaryobjects
primaryobjects / preorder-traversal-bst.js
Created January 24, 2017 02:58
Sum Root to Leaf Numbers in a binary search tree BST.
/* Preorder traversal
The idea is to do a preorder traversal of the tree.
In the preorder traversal, keep track of the value calculated till the current node, let this value be val.
For every node, we update the val as val10 plus node’s data.
*/
var sumNumberUtil = function(node, val) {
if (!node) {
return 0;
}
else {
@primaryobjects
primaryobjects / 1-hamming.js
Last active January 24, 2017 00:13
Calculate the hamming distance (number of different bits) between two integers.
/**
* @param {number} x
* @param {number} y
* @return {number}
*/
var hammingDistance = function(x, y) {
var result = 0;
var xbits = x.toString(2);
var ybits = y.toString(2);
@primaryobjects
primaryobjects / 1-missing-number.js
Last active March 31, 2021 00:59
Find the missing number in an array. Given nums = [0, 1, 3] return 2.
/**
* @param {number[]} nums
* @return {number}
*/
// Bit manipulation. Fastest.
var missingNumber = function(nums) {
var xor = 0;
var i = 0;
for (i=0; i<nums.length; i++) {
@primaryobjects
primaryobjects / first-bad-version.js
Last active January 23, 2017 20:01
Finding the first bad version in a chain of builds from an API. Optimize the API call to a minimum. Solved by divide and conquer.
/**
* Definition for isBadVersion()
*
* @param {integer} version number
* @return {boolean} whether the version is bad
* isBadVersion = function(version) {
* ...
* };
*/
@primaryobjects
primaryobjects / 1-isomorphic.js
Last active May 24, 2019 11:53
Isomorphic Strings
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isIsomorphic = function(s, t) {
var result = true;
if (s.length === t.length) {
var hash1 = [];
@primaryobjects
primaryobjects / paths.js
Last active January 23, 2017 00:23
Unique Paths, robot on path moving right or down in direction to goal.
var factorial = function(n) {
var result = 1;
for (var i=n; i>0; i--) {
result *= i;
}
return result;
}
@primaryobjects
primaryobjects / find-anagrams.js
Last active January 22, 2017 21:51
Find All Anagrams in a String.
/**
* @param {string} s
* @param {string} p
* @return {number[]}
*/
var findAnagrams = function(s, p) {
var result = [];
if (p.length <= s.length) {
// Build a hash of the letters.
@primaryobjects
primaryobjects / bst-delete-node.js
Created January 22, 2017 19:43
Delete a node in a Binary Search Tree BST.
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {number} key
@primaryobjects
primaryobjects / find-product.js
Last active January 23, 2017 00:43
Find a product within an array of numbers. Demo at http://jsbin.com/zidogenaro/edit?js,console
var mathManager = {
findProduct: function(product, arr) {
var result = null;
for (var i=0; i<arr.length; i++) {
for (var j=i+1; j<arr.length; j++) {
if (product === arr[i] * arr[j]) {
result = [arr[i], arr[j]];
break;
}
@primaryobjects
primaryobjects / mergesort.js
Last active January 20, 2017 23:53
Merge Sort implemented in javascript. Demo at http://jsbin.com/hinotelaca/edit?js,console
function mergeSort(arr) {
var result = [];
// Calculate mid point to split array.
var mid = Math.floor(arr.length / 2);
var left = arr.slice(0, mid);
var right = arr.slice(mid, arr.length);
// If more than one element, split the array again, until we get to a single element (implicitly sorted).
if (left.length > 1) {