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!
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.
” 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:
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 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.
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:
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:
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:
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. 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!
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?