As promised in a previous article, I’m going to start talking on this blog about the web development interview process and how you can overcome it with success.

During this series, I won’t only provide you detailed articles about how to solve common programming problems, but also useful tips that might be useful when confronting yourself with an interviewer.

So here we are, starting with the first problem: string reversal!

Initial setup

As always, before starting to dive into a new project we need to make some setup on our machine to make sure we have everything under our belt to make a great job. My advice is simply to create a .js file in a “coding problems” folder on your machine. Once you are done, we can start to tackle the problem.

Problem Statement

This is one of the most common interview problems that an interviewer might ask you to solve. It requires you to simply reverse the value of a string and, as you can imagine, there are multiple ways to do that in JavaScript. I will illustrate 3 of them to you by explaining exactly what they do and what kind of approach we are taking when writing a specific solution. Let’s start with the problem statement:

” Write a function that takes a string as its only argument and returns another string containing the reversed value of the function argument “

This means that, if we write a function called reverseString(str), the following input tests would look something like this:

  • reverseString(“hello world”); =====> “dlrow olleh
  • reverseString(“keyboard”); =====> “draobyek
  • reverseString(“overflow”); =====> “wolfrevo

Now that we have declared our target for this article, let’s start by analyzing three of the possible solutions we could use to solve it.

#1 A Simple, Language Proof Solution

This is the approach I would usually prefer if I was working on a personal project or simply coding at my daily programming job. It involves using a set of Javascript built-in functions that will make our life extremely easy. Let’s analyze this approach first and then we will take a look at its benefits:

Easily reversing a string using reverse()
Easily reversing a string using reverse()

This solution involves using the following functions:

  • Split(‘’): which will as you can imagine, split our array into an array of substrings. Notice that, if an empty string is used as the separator, the string is split between each character.
  • Reverse(): this is the real core of this solution, this function will reverse on place the content of an array so that the first element will become the last and vice-versa.
  • Join(): this function will join all the elements of an array into a string that gets returned as the result.

That was easy right? With just a line of code, we were able to perform the requested operation. Let’s try to analyze its benefits:

  • Quick: our solution was extremely quick to implement.
  • More Secure : our Solution makes use of built in Javascript functions which have been tested deeply and that provide us an instant tool to use.
  • Short: also thanks to the chaining function notation, we were able to implement our function with only a line of code!

#2 Loopy solutions

This second solution involves the use of a loop to perform the request operation:

Using a loop to reverse a string
Using a loop to reverse a string

Different solution, still very easy. Here is the explanation in detail:

  • By using a decrementing loop, we start from the last element of the array going down to the first.
  • For every element that we iterate through, we add that to a variable called reverse, which will then be returned from the function at the end of the loop.

The main advantages for this solution is that, even if it’s a little more complicated, it’s still an easy solution that will show to the interviewer that know how to think with loops.

The main disadvantage is that, since we are using more instructions and a loop, this solution tends to be more error-prone. A quick way to fix it would be to implement it using the for..of instruction newly introduced into the language:

Using the for of  loop to reverse a string
Using the for of loop to reverse a string

This will greatly reduce the possibilities of writing errors while still showing that you know how to tackle this problem!

#3 Again and again

The third solution makes use of recursion to solve this problem:

Using recursion reverse a string
Using recursion reverse a string

This solution basically involves calling the reverseString() function multiple times until we run out of characters. To reverse the string, the first character of the argument will be taken out of the string on each call and then we add all the letters taken out when our recursive function halts by using + str[0]. So that the execution of this function will look something like this:

The advantage of this function is mainly that it will help you to show to the interviewer that you can come up with many different solutions to solve a problem!

Conclusion

Reversing a string is easy, but the different ways in which we can tackle this problem can sometimes become daunting to a novice programmer. Hope this article helped you to get a new algorithm under your belt.
Ready for the next one?

LEAVE A REPLY

Please enter your comment!
Please enter your name here