DEV Community

Cover image for JS Coding Question #2: Reverse a string [Common Question - 3 Solutions]
Let's Code
Let's Code

Posted on • Updated on

JS Coding Question #2: Reverse a string [Common Question - 3 Solutions]

Interview Question #2:

Write a function that reverses a stringβ“πŸ€”

If you need practice, try to solve this on your own. I have included 3 potential solutions below.

Note: There are many other potential solutions to this problem.

Feel free to bookmark πŸ”– even if you don't need this for now. You may need to refresh/review down the road when it is time for you to look for a new role.

Code: https://codepen.io/angelo_jin/pen/LYyvBKy

Solution #1: Array methods

  • very simple solution that will utilize array methods to reverse the string.
function reverseString(str) {
    return str.split("").reverse().join("");
}
Enter fullscreen mode Exit fullscreen mode

Solution #2: Array forEach

  • will cycle through each characters and push it on the temp variable created one by one in reversed order.
function reverseString(str) {
    let reversedString = ''

    str.split('').forEach(char => {
        reversedString = char + reversedString
    })

    return reversedString
}
Enter fullscreen mode Exit fullscreen mode

Solution #3: Array reduce

  • slightly better than second solution above. Will use reduce and add the result to the empty string in reverse.
function reverseString(str) {
    return str.split('')
        .reduce((prev, curr) => curr + prev, '')
}
Enter fullscreen mode Exit fullscreen mode

Happy coding and good luck if you are interviewing!

If you want to support me - Buy Me A Coffee

In case you like a video instead of bunch of code πŸ‘πŸ˜Š

Discussion (6)

Collapse
lukeshiru profile image
LUKESHIRU

Consider that your solution wouldn't work with strings containing unicode characters such as emoji, so, for example:

reverseString("hello πŸ§‘πŸ»β€πŸ’»"); // "πŸ’»β€πŸ»πŸ§‘ olleh"
Enter fullscreen mode Exit fullscreen mode

To address this, you have a new proposal called Intl.Segmenter which will work as expected:

const unicodeReverse = string =>
    [...new Intl.Segmenter().segment(string)]
        .map(({ segment }) => segment)
        .reverse()
        .join("");

unicodeReverse("πŸ‘¨πŸ»β€πŸ’» hello"); // olleh πŸ‘¨πŸ»β€πŸ’»
Enter fullscreen mode Exit fullscreen mode

Cheers!

Collapse
frontendengineer profile image
Let's Code Author • Edited

thanks for the complete solution! definitely appreciate your time adding this in and spreading the knowledge.

I would be very surprised if I get asked to take unicode character into account in an interview though.

Collapse
lukeshiru profile image
LUKESHIRU

I would be surprised if in a 2021 interview I got asked to reverse a string, if I'm being honest. But if someone ask you to do it, is good to at least know that there are limitations when working with unicode strings.

Thread Thread
frontendengineer profile image
Let's Code Author • Edited

definitely a good possibility in my opinion. I got asked to code it on my last job but it is not to extreme like accounting for unicode. It was something quick and straight to the point. I imagine myself to not continue the interview if interview would go to this extreme. LOL

I wrote an article about FIzz Buzz as well, should be in this series and I was shocked to find out he was asked about it on his recent interview.

Maybe junior level can be asked perhaps?

Thread Thread
lukeshiru profile image
LUKESHIRU

Companies are moving away from this kind of questions in interviews because they don't tell you anything about the candidate, you're just testing their memory instead of their actual skill (similar to how school exams do). Anyone can memorize a FizzBuzz or the definition of closure, so you can't actually measure seniority based on that. Nowadays interviews focus in real world scenarios, allowing the candidate to google stuff when needed, like we all do everyday, and even have the exercises as "take home assignments" because in the the real world you wouldn't have devs watching you while you code.

Thread Thread
frontendengineer profile image
Let's Code Author

I totally agree with you. Might be more accurate to say companies HAVE moved away from FizzBuzz interview questions.