Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
String reversal is a very common interview question, and luckily itâs quite simple. There are many different ways to do this, but here are four methods, starting from the simplest and going to the most complex.
Use arrays
This is a very straightforward method that leverages existing language features in order to accomplish a task. Most interviewers would ask you to perform the same task in a more âmanualâ way if you tried this, but it demonstrates an awareness of Javascript, and is what Iâd use in real life to accomplish this.
Javascript allows you to call the reverse method on arrays. You can see some examples in the documentation here.
So we would create a function that does the following:
- Converts the string to an array
- Reverses the array
- Transforms the array back into a string and returns it
reverse = str => { return str .split("") .reverse() .join(""); };
Calling reverse('Ruairidh') would return hdiriauR for example.
Use a loop
This is more of a classic interview answer and reverses the string in a more manual way. All you need to do is loop through each character in the string, and reverse its order.
Take a look at the following code:
reverse = str => { let reversed = ""; for (let character of str) { reversed = character + reversed; } return reversed; };
All we are doing here is creating an empty reversed string in which we will push our characters.
Then we loop through the string we receive using an ES6 for...of loop (see the docs) and pushing each character to the reversed string.
So if we had hello then we would loop through and add h to a blank string. Then our next character is e which would be added to our string which has now become eh. Run in sequence you get:
This is pretty straightforward and has a time complexity of O(n).
Using Javascript helpers
Reduce is a Javascript array method which is really useful. I would use this in an interview to show that Iâm aware of ES6 features, and it has the benefit of some quite compact syntax.
If you havenât seen reduce() in action then itâs just a way to run a function on each item of the array and return a single value at the end. You can read a bit more in the docs.
As for a code example, it looks a little like this:
reverse = str => { return str.split("").reduce((rev, char) => char + rev, ""); };
So there are a few things going on here. Firstly, we convert our string to an array using split. Then we reduce each item of our array and in the callback we provide our accumulated results so far (rev), then we add the next character in the array to rev. We also shorten the syntax through arrow functions and an implicit return.
The logic is actually exactly the same as our loop above, but we use javascript to do some of the heavy lifting.
Conclusion
As you have seen above, itâs actually very simple to reverse a string once you have seen a few examples. Any of the above will accomplish your task though I recommend the last two for an interview.
Personally I prefer the second option as it shows your working a bit more than the last option which relies more on ES6 features.
Got another way to reverse a string? Tweet me!. Or try your hand at reversing strings on LeetCode.
Originally published at ruairidhwm.github.io on January 2, 2019.
String Reversal in Javascript was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.