Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:

Maya Shavin mayashavin

:octocat:
View GitHub Profile
@mayashavin
mayashavin / Queue.js
Last active Mar 5, 2018
Data Structures - Queue Implementation in JS
View Queue.js
function Queue(){
var storage = {},
head = 0,
tail= 0;
return {
enQueue: function(item){
storage[tail] = item;
tail++;
},
@mayashavin
mayashavin / Stack.js
Last active Jan 10, 2018
Data Structures - Stack in JS without using Array - Full Demo: https://jsfiddle.net/dpnminh/g1xy22cz/
View Stack.js
function Stack(){
var stack = {};
var stackSize = 0;
return {
push: function(item){
stack[stackSize] = item;
stackSize++;
},
pop: function(){
View DeepEqual.js
function deepEqual(a, b){
if (typeof a === 'object' && a !== null && typeof b === 'object' && b !== null){
var isEqual = Object.keys(a).length === Object.keys(b).length;
if (isEqual){
for (var key in a){
isEqual &= b.hasOwnProperty(key);
if (isEqual){
isEqual &= deepEqual(a[key], b[key]);
View TableGridDraw.js
//Input: Array of Arrays of Cells
var grid = [['name', 'height', 'country'],
['Kilimanjaro',5895, 'Tanzania'],
['Everest',8848,'Nepal'],
['Mount Fuji',3776,'Japan'],
['Mont Blanc',4808,'Italy/France'],
['Vaalserberg',323,'Netherlands'],
['Denali', 6168,'United States'],
['Popocatepetl', 5465,'Mexico']];
View SinglyLinkedList.js
function Node(value) {
this.value = value;
this.next = undefined;
}
function SLinkedList() {
var head = undefined;
var length = 0;
return {
View DoublyLinkedList.js
function Node(value) {
this.value = value;
this.next = undefined;
this.prev = undefined;
}
function DLinkedList() {
var head = undefined;
var tail = undefined;
var length = 0;
View PermutationCheck_Solution1.js
function isPermutation(strA, strB){
var isValid = true;
//First validity check
if (!strA || !strB || strA.length !== strB.length){
return !isValid;
}
//Get the map of characters and their occurences in strA
var charsMap = getMap(strA);
View PermutationCheck_Solution2.js
function isPermutation(strA, strB){
var isValid = true;
//First validity check
if (!strA || !strB || strA.length !== strB.length){
return !isValid;
}
var sortedA = sort(strA), sortedB = sort(strB);
@mayashavin
mayashavin / Emitter.js
Last active Feb 1, 2018
Emitter Class Implementation
View Emitter.js
function Emitter(){
var events = new Map();
return {
subscribe: function(event_name, callback){
//If events has event_name, then get the event Subscription and push to it.
//Else create new event, subscription and add to events.
var isEventExisted = events.has(event_name), index = 0;
View IdenticalDOMNode.js
//Given 2 identical DOM trees (but not equal) and one element of the first DOM tree,
//how would you find this element in the second DOM tree
//Questions to ask:
//1. Input: input node is inside DOM tree? Or need to check if it exists?
//2. Output node needs to be identical? Same tag?
//3. Are comments, texts considered as child nodes? - Yes, need to use childNodes, No, use children.
var sameLevelNodeIdenticalTrees = {
getIndexOf: function(nodeList, node){
return Array.prototype.indexOf.call(nodeList, node);
},