Algorithms for Beginners

I graduated from my software engineering program, now what? I am certain I am not the only person to (repeatedly) ask myself that in the days or weeks following a bootcamp. Bootcamps are intense, provide some structure, allow you to follow a curriculum, and offer lectures that help you stay connected and on track. When you graduate, you are suddenly flung from the nest of comfort and need to test out your wings in the great unstructured sky. It can be overwhelming to say the least.

For me, I felt like I couldn’t get my wings to work for the first few days. How should I structure my time? What’s my new routine look like? And HOW AM I GOING TO EVER UNDERSTAND ALGORITHMS???

Here is what didn’t work:

  • Try to get more done in a day than I had time for
  • Jump into algo challenges that weren’t at an “easy” level
  • Go through my day without a structure or routine

What worked? Writing out my new routine and starting at the beginning with learning algorithms.

Starting at the beginning meant taking my time with the most basic algo problem and spending time understanding/practicing how to solve it.

  1. Make sure I understand the problem being presented
  2. Write my own test cases to account for a variety of inputs
  3. Pseudocode it and break it down step-by-step
  4. Code
  5. Clean it up and consider if I need to optimize my code or not

Here is an example of following these steps with a VERY basic algo problem: reversing a string. Getting these steps down with a problem you can easily manage will mean that the steps come more naturally as the problems become more complex. Let’s walk through the steps together with this problem:

“Reverse a string. Your method will receive one argument, a string, and be expected to output that string with its letters in reverse order. Do not call any type of built-in reverse method! Please solve the problem using iteration.”

Step one: Taking time to understand the problem. This means reading the problem and test cases carefully, reading it all again, and rewriting everything in your own words:

//write a function that takes in a single argument, a string, and returns that string with all characters in reverse order. If I pass the string “hi” into my function, I expect my function to output “ih”

Step two: Writing my own test cases. Once you understand the problem and why answers to the test cases are what they are, write your own test cases. Do this so that you include inputs that the provided cases don’t account for and to test if expected answers match the actual answers.

The test cases provided:

Here are a couple of the test cases I added to account for spaces and other characters:

Step three: Pseudocode time!This means just writing how to solve the problem as a simple description in plain old English (or your primary non-coding language). Break it down and go step by step.

// initialize an empty array called result

// turn the string into an array so that I can iterate over each element/

character in the string

// push each element into result in reverse order

// turn the result array back into a string called reversed

// return reversed

Step four: Code! Turn your pseudocode into code with the goal to pass the test cases and be able to shout, “it works!!” BE ABLE TO EXPLAIN YOUR SOLUTION! Here is my working code, console.logs and all. I will break it down to explain each piece, step by step.

// I need to initialize a new array because I will eventually need an empty array to store elements in as I loop through my original array.

let result = []

// In order to use a loop, I need to make my string into an array so that I can iterate through it. I will test it first with the .split() method. In javascript, this method can accept up to two parameters: a separator and a limit. The separator specifies the pattern of how you want your string to be split up, i.e. where each split will be inserted. If you don’t include a separator parameter — .split() — the entire string is returned inside an array, but is not seperated. If you specify an empty string as the separator — .split(“”) — the string will be split up between every character in your string. I am not using the limit parameter in this case.

let array = str.split(“”)

// I am using a for loop to iterate through my array. A for statement looks like this:


// initialExpression determines the start of my loop. Since I am working in reverse, I start at the end of the array. If I was looping in a forward manner, I could set i = 0, essentially initializing a loop counter, and loop through until I reach array.length. But going backwards and starting at the end of my array, I set i = array.length -1 because the index of an array always starts at 0. Basic review, but important to understand. Let’s use an example of an array of strings instead of characters to clearly illustrate length.

let array = [“figure,” “it,” “out”]

array.length would give me 3 as we can see there are three words in this array. The indices though are 0, 1, 2. Therefore to begin my iteration at the element with an index of 2, I need to use the length of the array and subtract 1.

let i = array.length — 1

// Not only do I need to loop through my array starting at the end, but I also need to keep going in reverse order until I get to the beginning of my array. This is my conditionExpression (a condition that is evaluated at every iteration and if the value of conditionExpression is true, the statement portion will execute, but if the condition is false, the for loop will terminate. Since I want my loop to continue for the length -1 until 0, my conditionExpression looks like this:

i >= 0

Since I am decrementing in my loop, subtracting 1 from the index with each loop until I get to the beginning of the array, instead of incrementing (i++), my incrementExpression will be:


Putting this all together, the first line of my for loop is:

for (let i = array.length — 1; i >= 0; i — )

We are getting there.

//Next the statement. I need to store each element as I iterate. array[i] will represent each element at its respective index as I loop through the array. Let’s recap with a new example.

Say my initial string is ‘Hello World!’

After I turn my string into an array using.split(‘ ’) my array =[ ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘ ‘, ‘W’, ‘o’, ‘r’, ‘l’, ‘d’, ‘!’]

On the first loop through, array[i] is ‘!’. With each iteration of the loop, I can use the .push() method to literally push each element into my new array.


// Now I have a reversed array [ ‘!’, ‘d’, ‘l’, ‘r’, ‘o’, ‘W’, ‘ ‘, ‘o’, ’l’, ‘l’, ‘e’, ‘H’] which I can turn back into a string using the .join(“”) method. This creates a new string by concatenating, or linking together, the elements in the array attached to the .join().

let reversed = result.join(‘’)

// Remember to always note your expected output and state what the problem wants you to do. In this case, I want to return my reversed string.

return reversed

Output is ‘!dlroW olleH’

Step Five: Clean it up and consider if I need to optimize my code or not. When your solution is accepted or is working in all the test cases, refactor it to be clean and dry. Test it again. If your test isn’t getting hung up because it is taking too long, you weren’t asked to optimize it, or you don’t feel like optimizing just for the heck of it, then don’t. Here is my slightly cleaned up, ES6ed a bit, code:

Remember learning algorithms is like being in a whole new world. Enjoy it when you can. Start at the beginning, get down the basics, and practice the problem solving approach that works for you. Celebrate your win, take a break, consider writing a blog on your steps, and move on to your next goal for the day.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store

Software Engineer, mother, and former therapist. I am a runner, reader, chai tea drinker, nature lover, and information nerd.