Algo a Day
HomeProjectsGithubAbout

Exploring algorithms in JavaScript, Go, and Java

The goal of this project is to explore a different algorithm every day, try it out in JavaScript Clojure Java, and Go, and record my experiences doing so. I will discuss use cases, constructing the algorithm in different languages, and things I learned throughout the experience.

Update: As I find my interests lie mainly in the back end of development, I've decided to change my language focus from Clojure to Java, to better equip me for success when working with the back end.

Algorithms

Largest product of three numbers in a list
a month ago

Given a list of numbers, return the largest product that can be made using three of the numbers in the list. The only way that negatives can be involved in creating the product is if there are two large negatives. Sorting the array would put those large negatives at the front and smaller negatives at the end. If we are using negatives in the answer, we would only use the two largest negatives and the largest positive. If the answer uses only positives, all those values will be at the end of the sorted array. Thus, we can compare the result of a possible solution involving negatives and a possible solution using positives, and return the largest of those two.

Invert a tree
a month ago

By "invert", we mean take a tree and swap the positions of the children, so that the branches are swapped. Left becomes right, right becomes left. See below for an example.

Setting up a simple server
a month ago

I'm going to take a little departure from algos today and instead talk about setting up a simple server in JS and Go. I won't be addressing how to do so in Java, as the most common practice relies on a framework (i.e. Spring) that is basically its own ecosystem.

Ways to get up a staircase
a month ago

Given a staircase of n length, and assuming that a person can either ascend 1 or 2 stairs in a single step, determine the possible number of ways that a person can get up the stairs.

2D matrix rotation
a month ago

Rotating a matrix 90 degrees means that all rows becomes columns in the new matrix. It is easiest to think about the respective indexes for an element as coordinates, which means that rotating the matrix is as simple as reflecting the matrix across two planes: the horizontal and the diagonal (which is the same as flipping the coordinates so that (x,y) becomes (y,x)).

Queue
a month ago

Today I will only be implementing the Queue data structure, because I am just learning Java and will be adding it in to the languages that I write my algorithms in! Wahoo! So, we will implement a simple "first in first out" (FIFO) queue in JavaScript, Go, and Java.

Binary search tree equivalency
a month ago

A binary search tree is simply a tree where the left child of a node (A) has a value that is less than A's value, and the right child has a value that is greater than A's value. These rules apply throughout the entire tree. I will not go into the advantages of a BST, nor will I discuss the importance of balancing the tree, but note that it is quite important! An unbalanced BST might as well be a regular tree.

Is power of two?
2 months ago

This simple algorithm can be accomplished using a classic `while` loop, recursively dividing by 2 until we are at a number less than or equal to 2, then checking if that number is 2. If it is, the number is a power of two!

Breadth-first search
2 months ago

A breadth-first search is used in a tree and is categorized by checking every node at a given depth, left to right, and only once all the nodes at the current level have been checked do we descend to a deeper level.

Caesar cipher
2 months ago

The Caesar cipher is a basic cryptography algorithm where each letter of the alphabet corresponds to another letter that is offset by a predetermined amount. For instance, if the offset is 3, we would get the following conversions: A -> D, B -> E, ... Y -> B, Z -> C (note how we wrap back around if the cipher takes us to either end of the alphabet).

Depth-first search
2 months ago

Depth-first search of a tree is a method for recursively iterating across all nodes of a tree to find if the tree contains a given value. The depth-first method descends to the lowest level of each branch before moving to a different branch.

Jump game
2 months ago

Given an array (vector in Clojure) where each element represents the maximum jump distance (number of indexes to travel), determine if you can reach the last index in the array when starting at the 0th index.

Pascal's Triangle
2 months ago

Pascal's triangle is the triangle formed when adding the numbers immediately above to the right and above to the left. See the image below, which is taken from Wikipedia:

Quick sort
2 months ago

Quick sort is one of the better sorting algorithms out there, with an average time complexity of O(n*log(n)). Quick sort recursively rearranges items in a list based on the index (before or after the pivot point) and the value of the element (above or below the value at the pivot point).

Jump Search
2 months ago

Jump search (otherwise known as block search) is a searching algorithm for a sorted array, similar to a binary search. However, instead of comparing if the searched value is above or below the midpoint - like in a binary search - we are checking a "block" at a time and seeing if the value *should* be in that block.

Straight traversal of a linked list
2 months ago

Today's prompt is the straight traversal of a linked list. I will assume the prompt is: "Given a value, return the node if the value exists in the linked list, otherwise return null."

Sorting algorithms
2 months ago

There are a multitude of sorting algorithms, each using a different approach for modifying some list such that lower values are before higher values.