Technical Interview Challenge #1: Reversing a String*

To start with the most basic interview question which is how to reverse a string, array, etc let the fun begin!

The obvious solution that comes to mind is to use several basic built-in methods, which are simple to use but most likely not allowed during our tech interview, but for those who might not be familiar with these methods here you go!

In the below example, we are using .split() method to convert our string into an array so we can use .reverse() method which is pretty self-explanatory and then we use .join() method to convert back from the array into the string.

function reverse(str) {
return str.split('').reverse().join('')
}

Knowing that most likely we won’t be allow to use built-in methods, let’s try to achieve the same outcome without the .reverse() method.

  let reversed = '';
for(let char of str) {
reversed = char + reversed
  } 
return reversed;
}

In the above example, we use for loop method where we store individual letters/characters in our ‘char’ variable and then add individual characters into our ‘reversed’ variable in the reversed order.

We also could use the “forEach” method and using an arrow function. We are calling a function on each character in our string. In our case, ‘char’ is our function’s argument and this function is called on every element in our array(=> don’t forget that in order to use ‘forEach’ we converted our string into an array by calling .split() method on our argument(string)

function reverse(str){
let reversed = ''
str.split('').forEach(char => {
reversed = char + reversed
})
return reversed;
}

Now, let’s try to tackle our second data structure which is an Integer.

What we want to achieve here is very similar to reversing a string with a little addition, however, we will be using several methods to deal with the fact that we are working with positive and negative numbers. Our goal is to build a function that will produce the results below:

Examples:
reverseInt(15) // 51
reverseInt(-982) // -289
reverseInt(6000) // 6

Do you have suggestions on how to solve it? Let’s try it together…

function reverseInt(int) {
let result = int.toString().split('').reverse().join('');
return parseInt(result) * Math.sign(int)
}
reverseInt(500)

I want to focus here on how to deal with positive/negative numbers after we reverse the actual numbers so for simplicity reasons, I used .split(), .reverse() and .join() methods but you can achieve the identical result by using any of the other two examples I introduced you to when discussing how to reverse strings.

Basically, we are using .Math.sign() method which returns -1 if an argument(in our case int) is less than 0 and 1 when > 0. This method is used on numbers, so we need to convert our string into a number again by using ‘parseInt’. Fun!

I also wanted to share with you a quick and easy way to test your code in your terminal:

First, place a word “debugger” in your code, for example in below example it makes sense to place our debugger inside our for loop so we can view individual char (our characters) and well as our “reversed” string:

function reverse(str) {
let reversed = '';
for(let char of str) {
reversed = char + reversed
debugger;
} 
return reversed;
}
reverse("bingo")

Ok, after placing our debugger let’s open our terminal inside our editor and type:

node inspect YOUR_FILE_NAME (for example: node inspect index.js)

You should see your debugger being executed for the first time:

Now, if you type ‘repl’, you can test your code inside your loop, like checking what your “char” and reversed return:

By typing “char” we can see the very first letter from your string, in order to see the next char you need to press “Control + C” and then “C”, then again “repl”.

Stay calm and keep coding!

*methods and examples introduced by Stephen Grider at “The coding interview Bootcamp”

Like what you read? Give Kasia Rosenberg a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.