Skip to content

Instantly share code, notes, and snippets.

View mitrakmt's full-sized avatar

Michael Mitrakos mitrakmt

View GitHub Profile
@mitrakmt
mitrakmt / translateRomanNumerals.js
Last active September 26, 2016 05:16
Translate Roman Numerals programming interview challenge.
function translateRomanNumeral (romanNumeral) {
var DIGIT_VALUES = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
@mitrakmt
mitrakmt / binarySearchTree.js
Created September 21, 2016 15:11
Implement a Binary Search Tree in JavaScript
// This Binary Search Tree is implemented using the prototypal pattern
var BinarySearchTree = function(value) {
var instance = Object.create(BinarySearchTree.prototype);
instance.value = value;
// a BST where all values are higher than than the current value.
instance.right = undefined;
// a binary search tree (BST) where all values are lower than than the current value.
instance.left = undefined;
@mitrakmt
mitrakmt / evenOccurrence.js
Last active September 26, 2018 16:56
Exploring the programming interview question even occurrence in JavaScript.
// Prompt: Find the first item that occurs an even number of times in an array. Remember to handle multiple even-occurrence items and return the first one. Return null if there are no even-occurrence items.
function evenOccurrence (array) {
// Store counts
var storage = {};
// Store each value within the storage object to keep count
array.forEach(function(value, index) {
storage[value] = storage[value] + 1 || 1;
@mitrakmt
mitrakmt / permutations.js
Created September 20, 2016 15:09
All permutations of a set interview challenge.
function getAllPermutations(string) {
var results = [];
if (string.length === 1) {
results.push(string);
return results;
}
for (var i = 0; i < string.length; i++) {
var firstChar = string[i];
@mitrakmt
mitrakmt / queue.js
Created September 19, 2016 03:42
How to implement a Queue in JavaScript
// This Stack is written using the pseudoclassical pattern
// Creates the queue
var Queue = function() {
this.storage = {};
this.count = 0;
this.lowestCount = 0;
}
// Adds a value to the end of the chain
@mitrakmt
mitrakmt / stack.js
Created September 19, 2016 03:27
Stack Data Structure in JavaScript
// This Stack is written using the pseudoclassical pattern
// Creates a stack
var Stack = function() {
this.count = 0;
this.storage = {};
}
// Adds a value onto the end of the stack
Stack.prototype.push = function(value) {
@mitrakmt
mitrakmt / firebase_snapshot_parent.js
Last active September 2, 2015 04:42 — forked from anantn/firebase_snapshot_parent.js
Firebase: Get the parent of a snapshot.
function getParent(snapshot) {
// You can get the reference (A Firebase object) from a snapshot
// using .ref().
var ref = snapshot.ref();
// Now simply find the parent and return the name.
return ref.parent().name();
}
var testRef = new Firebase("https://example.firebaseIO-demo.com/foo/bar");
testRef.once("value", function(snapshot) {
@mitrakmt
mitrakmt / firebase_remove_pushed.js
Last active September 2, 2015 04:42 — forked from anantn/firebase_remove_pushed.js
Firebase: Removing an item you've pushed. This snippet shows how to remove an object that you just added using push().
function pushSomething(ref) {
// Let's push something. push() returns a reference that you can hold onto!
var justPushed = ref.push({test: "push"});
// We return a reference, but you can also return the name of the newly
// created object with .name().
return justPushed;
}
function removeItem(ref) {
// Now we can get back to that item we just pushed via .child().
@mitrakmt
mitrakmt / firebase_player_assignment.js
Last active September 2, 2015 04:41 — forked from anantn/firebase_player_assignment.js
Firebase: Assigning players in a multiplayer game. This snippet assigns you a spot in a game if the game isn't full yet.
function go() {
var userId = prompt('Username?', 'Guest');
// Consider adding '/<unique id>' if you have multiple games.
var gameRef = new Firebase(GAME_LOCATION);
assignPlayerNumberAndPlayGame(userId, gameRef);
};
// The maximum number of players. If there are already
// NUM_PLAYERS assigned, users won't be able to join the game.
var NUM_PLAYERS = 4;
@mitrakmt
mitrakmt / firebase_create.js
Last active September 2, 2015 04:41 — forked from anantn/firebase_create.js
Firebase: Creating data if it doesn't exist. This snippet creates a user only if it doesn't already exist.
function go() {
var userId = prompt('Username?', 'Guest');
var userData = { name: userId };
tryCreateUser(userId, userData);
}
var USERS_LOCATION = 'https://SampleChat.firebaseIO-demo.com/users';
function userCreated(userId, success) {
if (!success) {