Skip to content

Instantly share code, notes, and snippets.

@alexhawkins
alexhawkins / lastSurvivor.js
Last active August 29, 2015 14:14
Last Survivor Coding Question
'use strict';
/** APPLICANT INFO ***********************************************************
------------------------------------------------------------------------------
* Name: Alex Hawkins
* Email: [email protected]
* GitHub: github.com/alexhawkins
* LinkedIn: linkedin.com/in/alexhawkinsme/
****************************************************************************/
@alexhawkins
alexhawkins / HashTable.js
Last active August 7, 2021 23:33
Correct Implementation of a Hash Table in JavaScript
var HashTable = function() {
this._storage = [];
this._count = 0;
this._limit = 8;
}
HashTable.prototype.insert = function(key, value) {
//create an index for our storage location by passing it through our hashing function
var index = this.hashFunc(key, this._limit);
@alexhawkins
alexhawkins / stackNqueues.js
Last active August 29, 2015 14:08
Functional, Functional-Shared, Prototypal, Pseudoclassical implementations of Stacks and Queues in JavaScript
/******************************************************/
/******************************************************/
/**
* FUNCTIONAL QUEUE
*/
/** Queues are FIFO, first in first out */
'use strict';
var makeQueue = function() {
@alexhawkins
alexhawkins / shoppingCart.js
Last active September 7, 2022 08:29
Example of a Module Design Pattern in JavaScript
/*MODULE PATTERN*/
//create a namespace
var shoppingCart = (function() {
//private variables
var total;
var basket = [];
//private methods
@alexhawkins
alexhawkins / hashTable.js
Last active July 25, 2024 03:38
A Simple Hash Table in JavaScript
/*HASH TABLE - a dictionary/hash map data structure for storing key/value pairs. Finding
an entry in a hash table takes O(1) constant time(same for 10 as 1 billion items). Whereas
finding an item via binary search takes time proportional to the logarithm of
the item in the list O(logn). Finding an item in a regular old list takes time proportional to
the length of the list O(n). Very slow. Hash Tables = very fast */
var makeHashTable = function(max) {
var storage = [],
hashTableMethods = {
@alexhawkins
alexhawkins / stackDataStructures.js
Last active September 4, 2016 09:47
Stack Data Structures, Functional, Functional Shared, Prototypal, Pseudoclassical Implementations
/**********FUNCTIONAL - SHARED STACK IMPLEMENTATION ****************/
var makeStack = function() {
var stack = {
storage: {},
length: 0
};
extend(stack, stackMethods);
return stack;
@alexhawkins
alexhawkins / bubbleSort.js
Last active August 29, 2015 14:06
Various Bubble/Selection/Quick/Merge Sort Implementations
/* BASIC BUBBLE SORT ALGORITHM
1. Compare first item to the second item
2. Swap if first item should be after second
3. Compare second to third item
4. If second should be after third, swap
5. Continue until end of data set;
*/
//FAST BUBBLE SORT
@alexhawkins
alexhawkins / JSON.stringify.js
Last active August 29, 2015 14:06
Hardly Readable JSON.stringify Implementation
var stringifyJSON = function(obj) {
var objElements = [];
//check for literals
if (!(obj instanceof Object))
return typeof obj === 'string' ? '"' + obj + '"' : '' + obj;
//check for arrays
else if (Array.isArray(obj)) {
return '[' + obj.map(function(el) { return stringifyJSON(el); }) + ']';
//check for object if not array
} else if (obj instanceof Object) {
@alexhawkins
alexhawkins / searchAlgos.js
Last active September 4, 2016 09:46
Binary Search Algorithms with Shuffle and Range
/*
#DATA STRUCTURES & ALGORITHMS
A) Binary Search with RANGE and Shuffle algorithms:
-Algorithmic Steps
1) If needle == value in haystack, we're done. Exit.
2) If needle < middle value in haystack, go left. Go to step 1.
@alexhawkins
alexhawkins / recursionBasics.js
Last active August 29, 2015 14:06
Recursion Basics: How to Solve Problems Recursively
/* RECURSION ( when a function invokes itself to solve a problem )
All recursive functions have the following characteristics:
1) The method is implemented using if-else logic that leads to
different cases.
2) One or more base/terminating cases(the simplest case) are
used to stop the recursion ex. if (n === 0) stop
3) Every recursive call reduces the original problem, bringing
it increasingly closer to the terminating base case until