Data Structure and Algorithm Common Interview Questions (Part 1)
Tags: Data Structures, JavaScript, Palindrome, Algorithms, Reverse String

Reverse string, Palindromes, Reverse Integer and Return Max character into a string.

When I first start programming, I did know anything about Data Structure and algorithm, and even after completing a Coding Bootcamp, I was completely blank in the topic. I found out later that these are important topics to discuss, especially, for interviews. I decide to study those topics which I was trying to avoid until that moment because I was scared of the challenge I could face.

The good news was that after starting to study these topics I found them really funny and helpful in improving my problem resolution skills. Another good news is that the concepts are always the same, and has been like this for the last 30 years, so once you got this basic and more advance concept you are done, that’s it, you can solve many algorithms and data structure problem always using the same pattern.

In this series of blogs, I will post every week the more classic interview questions that cover all the different concept of Data Structure and Algorithm. I’ll start from the easiest one and go into the more advance. The structure of this blog is done in a way to help you understand the next and more advance one, so I will suggest that you read each blog to be able to improve your skills and knowledge to solve the more advance concepts later.

Reverse String

The reverse string is one of the most class questions and is really simple to understand. You are giving a function which takes a string for example “apple” you require to reversed “elppa”.

Solution 1

This solution uses a pre-build function “reverse()”. This method works on the array, so the steps to do here to solve this problem are:

Turn the string into an array, and split each character inside reverse the array join the array back into a string return the string

To turn the array into a string we going to use the “split(‘’)” method, we then going to call the “reverse()” one on the array and in the end we are going to use “join(‘’)” to join back the array into a string.

Using the concatenation, we can return everything on the same line.

This way to solve the reverse string is easy, but the problem is that during an interview does not show your skills or problem-solving thinking, and there isn’t much to discuss with the interviewer. This is why I will show you a better approach to show your skills in the interview.

Solution 2

Septs to solve the revered string:

create an empty string, and we are going to call it “reversed.” use a for loop to loop into the string we passed inside the method for each character into the string, we are going to add it to our empty string return the reversed string

So let’s examine how this works, in the first round of loop we have an empty array and assuming we have the string “apple” to reverse. We are going to take the first character “a” and combined with the string which is empty so the reversed string will become “a”. In the second round, we have the letter “p”, so the same we are taking “p” and add it to the reversed array that this time is not empty but has the “a” inside from the previous loop, so as follow:

p + a = pa

p + pa = ppa

l + ppa = lppa

e + lppa = elppa

By explaining this simple concept during the interview, you will show better skills in solving a problem than a simple call to a “reverse” method. Ther are other methods to solve the reverse string but you don’t need to know all of them, but I will list for you an additional one:

str.split('').reduce((a,b) => b + a)

This method transforms the string into an array and calls the reduce method on it which combine the letter in the order you ask to do it, in this case, “a” and “b” represent each letter if you call a + b will rejoin the string as before, but by reversing the b + a will do the opposite. This method is also perfect for doing calculation inside the array.

Palindromes

This method is really similar to the above one, and I will go over it really quickly, and the question is:

Given a string, return true if the string is a palindrome or false if it is not. Palindromes are strings that form the same word if it is reversed. *Do* include spaces and punctuation in determining if the string is a palindrome.

— — Examples:

1. palindrome(“abba”) === true

2. palindrome(“abcdefg”) === false

As you can see there is a clear pattern here and the solution is simple you can use the above method to reverse the string and compared to the original one and if are not similar return false otherwise return true:

As you can see, the above solution is really similar to the reverse string, but before to return the value we check if there are equal or not. In the second one, we check first if the first letter is equal to the last one, and in that case, we do not even run the function and return false directly.

Reversing an Integer

This problem may seem similar to the above two we did but is actually a bit more tricky, the problem is:

Given an integer, return an integer that is the reverse ordering of numbers.

— — Examples

reverseInt(15) === 51

reverseInt(981) === 189

reverseInt(500) === 5

reverseInt(-15) === -51

reverseInt(-90) === -9

const reverseInt = (int) => {}

I suggest you to give a shot to this one before to go ahead and see the solution.

Solution 1

As I said, this one is a bit more tricky for the simple fact that having a negative number we need to return instead to -15 -51.

The first solution has the following steps:

Transform the int into a string Use the split, reverse and join method to get the reversed result. Convert back the string into and Int Check if the original number was negative, n< 0. In case is negative multiple the new reversed result for -1, and return the result.

In the return statement, we are asking if n<0, if true we multiple that reveserdNumber * -1 and this will give us a negative result.

Explain: if we had an original argument of -15 after transforming and reversing the value we are going to have a string of “51-”. by re-transforming the value into an integer, we would lose the minus “-” sign. At this point, once we check if the original value was negative, we multiple this new value for -1 and we will get a negative -51.

Return Max character

This algorithm is a little bit more complex to code than to think about the solution:

Given a string, return the character that is most commonly used in the string.

— — Examples

maxChar(“abcccccccd”) === “c”

maxChar(“apple 1231111”) === “1”

Give it a shot before to check the below solution.

Solution 1

Step to take for solving this quiz:

Create an empty object Loop into the string and for each character into the string add it to the object as a key, if the key does not exist the first time you create it need to give a value of 1, but if in the next loop the key exists you increase by one the value. Check for each key into the object which one has the max value.

In the return statement, I am using Object.keys() which gave me an array of all the keys into the object. I then call the reduce method on each key and check if the obj[key] is bigger then the next one or not. In this case, reduce will check each key-value pair and return in case a or b. to better understand this method, check the documentation on the reducer() and the Object that keys.

Solution 2

create empty object create max variable with a value of zero 0 create a letter variable with a value of the empty string “ “ Loop into the string and for each character into the string add it to the object as a key, if the key does not exist the first time you create it need to give a value of 1, but if in the next loop the key exists you increase by one the value. Loop into the obj and check if the value of the current key is greater, then max will get the value of the current value of the key and letter will become the current key, in the case is not both max, and letter variable will remain the same. Return the letter variable

I hope this first blog on Algorithms and Data structure was helpful and clear, in case follow me to see more solution.