Algorithms and Data Structures Part I

Photo by Markus Spiske on Unsplash

This will be the first in a series of problems I will go over in as much detail as possible. Some of these, you may know and some you may have never seen before. Either way I hope they help prepare you for interviews and to feel more confident in your ability to problem solve.

If you’re like me and somewhat new to the coding world let me just tell you, STUDY ALGORITHMS!!! they are so important. Algorithms usually pop up in technical interviews and are used everyday by developers. So with that in mind let's get started…

For this problem you are given a string and asked to reverse it then return it as a new string. Ok so let’s get started. First we need a function…

Now there are a few ways to tackle this problem but this is the easiest way…

We will be using the method. This method is used to reverse an and we have been given a but luckily we have a method to turn our into an , reverse it and then turn it back into a

So we are going to:

  1. Turn ‘str’ into an array.
  2. Call the ‘reverse’ method on the array.
  3. Join the array back into a string.
  4. Finally return the result (Very Important!).

So here we create a variable that I’ve named and we take ‘’ and call on it. This will turn our into an . Now that we are working with an we can call that method…

and now that the is reversed we can turn that back into a with the method…

and we have a reversed . Lastly we need to return the result…

and that’s basically it!

We can tidy this code up a bit though and instead of creating a variable at all we can do the following…

Here we return our str with all called on it directly and we get the same result with three lines of code instead of five.

In part II, knowing what we’ve learned here we will talk about palindromes and integer reversal all using the method.

Thank you for reading.

Learning to code, one day at a time.

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