Algorithms Shouldn't Be Scary!

By:
Last Updated: March 20, 2020
Coding Dojo helps us understand the simpler side of Algorithms.

Algorithms have a bad reputation. The word itself isn't helping - it sounds technical. Algorithms can feel like a steep barrier of entry to clear for a job in tech, and often are perceived as not being commonly used afterwards. Or maybe you've added meaning to the word after seeing it crop up in a variety of media, whether it is a hacker on some TV show nerding out in front of some glowing screens or the most recent article on how AI will take over the world. I want to backup a little bit and just look at using algorithms as a way to continually develop the quality of the code you write.

Regardless of what they can do, it should be pretty evident that behind all of the hand-wavy-black-magic that can be associated with them, algorithms can be pretty powerful. A lot of that power comes from learning which algorithms or combination of algorithms to use at different times, the recent surge in machine learning research should be evidence enough of that. But there are some simpler and more mundane benefits to be derived from practicing and regularly engaging with algorithms. Investigating algorithms regularly can lead to a better understand how to express our process, improve how well we implement a solution, as well as evaluate and compare their effectiveness. They can help you improve your code quality.

They're…EVERYWHERE

Before we go much further, let's clarify what an algorithm is: An algorithm is a process or set of rules to be followed in calculation or problem-solving operations. That's not so bad! You've been using algorithms your whole life, you use them every day. I'll talk about a human-centric algorithm I use 4-5 times a week: What Am I Having for Dinner? This is a fascinating one, because even though I do roughly the same process every day, the "input" or information that I use to determine a solution changes every day! I won't go into too much detail, but I first consider one thing: Am I making food for myself or for company? After I solve that I'll look at whether I have any ingredients in my fridge that are nearing expiration, and I will try and prioritize using those. From there, I like to try and avoid using too many dishes or doing too much prep work.

There are a lot of neat things that arise when we take a moment to look at how we are solving problems. In the dinner example, there are many different factors that could play a role in what I ultimately end up eating. It can take all day to figure out what to eat (I know I still do...) so many people choose a few key things to focus on when choosing. Over time, what you observe is that you usually figure out dinner faster if you ask the right questions sooner. Do you want food sooner or later matters more than what's on the JavaScript subreddit. And thinking about how we approach solving basic problems can lead to neat insights that improve how we write our code, whether it's straightforward back-end logic or complicated data-processing. Our dinner example yields a simple lesson: put more probable and frequently reached branches of logic earlier in lengthy if/else chains.

Structured Code = Structured Process

When I first started coding and working with algorithms the processes of analyzing decisions and looking for ways to optimize or change how the process behaved to meet certain conditions was an interesting puzzle. But what I was really blown away by was what happened when the processes we use to solve problems started to have their own problems, and instead of some new twists in logic causing improvements, whole new ways of structuring and representing data became the best way to advance algorithm performance. And that is mind boggling to me, but I also store information differently than a computer.

These new data structures sometimes have commonalities with how we arrive at certain conclusions. One example I enjoy using is the underlying idea of a hash table. Hash tables are a powerful data structures that can allow for very fast access to data based off a given key. Hash tables are very good at telling you whether data is stored for a given key, and they use a process that I use every time I leave to catch a bus. I have to bring my keys when I leave, so I need to be able to quickly determine whether I have them. This is a very fast check for me, they would only ever be in one 2-3 places, so I check them and determine if I need to adjust my departure for a quick search or if I can head out the door. In essence I use a process to very rapidly identify where my keys should be and the 2 most likely places they would be if they aren't there. Computers do this using a piece black-mathematical-sorcery goodness called a hash function. In short, this data structure maintains a table full of data that is accessed by keys.

To get the data for a particular key, the data structure feeds the key into the hash function and a location in the table is spat out. This is the same bit of magic that goes on when I associate "keys" with "left pocket"! Sometimes you don't find your keys in your left pocket, so you immediately check your right pocket and then your jacket pocket. What is neat about these steps is that it takes checking 2-3 alternative places for you to reach a point where you feel confident saying "I don't know where my keys are".

Hash tables do this too! Using a process called open addressing, hash tables will "rummage around" to see if something might not be exactly where it is supposed to be and then stop after a few steps. By building data structures that are optimized around certain operations, we can find certain parts of existing processes and substitute a new data structure for a slower performing process.

Learning How to Learn

What I personally find exciting about algorithms is that beyond the immediate application of problem solving they can give insight into the decision-making process. I think this is fascinating and helpful to practice. Learning algorithms gives us many benefits, but I think that first among them is they give us practice writing a different language. Whether you are roughly getting your ideas on the page trying to write an initial solution or digging into the finer implementation details of certain methods to save time, practicing implementing algorithms improves your ability to structure code in more meaningful ways and to improve clarity and performance. Having better understanding of syntax and control of expressing yourself in different ways can be helpful outside of just algorithms, as I am sure you can imagine.

But outside of helping you express your solution to a problem or implement some process, exposure to algorithms and learning new data structures can help you express yourself more effectively or efficiently. When determining how to store different data types, knowing how fast different operations are for different structures should influence what you choose. Or maybe you have memory constraints or costly transactions that need to be considered. These kinds of decisions show up every day and often times it takes practice to learn how to smoothly integrate more complex concepts in 'readable' ways into your code. Learning about different syntaxes and methods of accessing and storing data can improve how well your code performs and help it more clearly communicate intended behavior.

One of the things that I enjoy the most about doing an hour of algorithms every day with 20 people is when people learn something they think they can use in their own code. Sometimes its a new syntax that they think makes loads more sense or a different data structure that has different methods and uses that improve the clarity of a certain process they do regularly. Regular time invested in developing either understanding of a language/paradigm or new data structures through algorithms, especially early on, helps to improve your ability write code.

Start Practicing, Now!

Hopefully at this point I've convinced you to try and approach algorithms in a different light, whether you are learning the basics, preparing for interviews, looking for small ways to improve your code, or just looking for a Wikipedia article to read. If you're on the fence or interested, I'd encourage you to go out and find multiple solutions to the same algorithm and compare them. This can seem a bit drab, but there are some fantastic stackoverflow posts, ebooks, subreddit posts, articles, and websites that do an excellent job of presenting the solutions and are well worth discovering.

I know it may seem crazy, but the word 'algorithm' is only going to show up more and having a better understanding of what they are and how certain types work will be helpful. If you are interested in looking into algorithms more, also look into related fields of math and science! The intersections between one's disciplines, algorithms and another can provide fascinating insights to problem solving as well. In this way algorithms can be more than just interview practice, can help you develop a deeper understanding of the technologies we use every day and improve the quality of your code in general.


This post was sponsored by Coding Dojo.

Would you like to learn more about Coding Dojo? Read what alumni have to say on SwitchUp.

Person thinking

Need help making a decision?

We'll match you to the perfect bootcamp for your location, budget, and future career.