Interview Prep Day — 6

Navneet Ojha
4 min readJul 15, 2020

--

All about problem solving and Maths

In continuation to below post

Today I did Java interview questions — related to JSP, Threads, and Exception Handling. Then I solved the leetcode July challenge problem, which was

Reverse Words in a String

Given an input string, reverse the string word by word.

Example 1:

Input: "the sky is blue"
Output: "blue is sky the"

Example 2:

Input: "  hello world!  "
Output: "world! hello"
Explanation: Your reversed string should not contain leading or trailing spaces.

Example 3:

Input: "a good   example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.

Note:

  • A word is defined as a sequence of non-space characters.
  • Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
  • You need to reduce multiple spaces between two words to a single space in the reversed string.

Solution: For reversing the string

  1. Convert the string to an array
  2. iterate the array from end index to the start index and append the values to the result string
  3. There is a catch we need to remove extra space, for that if there comes a space in between while iterating take a jump using continue.
  4. last but not the least trim, if there is an empty space left in start or end of the string

public String reverseWords(String s) {
String[] words = s.split(“ “);
int start = 0;
int end = words.length-1;
String result = “”;
for(int i = words.length-1; i >= 0 ; i — ){
if(words[i].equalsIgnoreCase(“”)){
continue;
}
result+=words[i]+” “;
}
return result.trim();
}

Reverse Integer: Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

Note:
Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

You may remember in your schools you may have done this type of question in math

  1. If you divide 123 / 10 we get 12 and remainder as 3, diving 12 / 10 we get 1 and remained as 2, 1/10 we get 1 as remainder if you see you have got the same digit in reverse order as remainders are 3 2 1 respectively, which is the answer we are looking for.
  2. For the negative integer, we can convert it to positive, and then after finding out the result we can convert the solution back to the negative sign.
  3. Checking if the reverse solution we got overflowed or not, if we compare the value of (reverse — n%10) with the previous reverse number it should be same and give us true in output, if not then return 0

public int reverse(int x) {
int prevX = x;
boolean flag = false;
if(x < 0){
flag = true;
x = -x;
}
int prevReverse = 0, reverse = 0;

while(x > 0){
int curX = x%10;

reverse = reverse * 10 + curX;

if((reverse — curX)/10 != prevReverse){
return 0;
}
prevReverse = reverse;
x = x/10;

}

return (flag) ? -reverse : reverse;

}

Then I did one of the easiest questions from maths, printing fizz for multiples of 3 and buzz for multiples of 5, if multiples of 3 & 5 then print both.

I did another question which was

Power of Three: Given an integer, write a function to determine if it is a power of three.

Example 1:

Input: 27
Output: true

Example 2:

Input: 0
Output: false

Example 3:

Input: 9
Output: true

Example 4:

Input: 45
Output: false

Follow up:
Could you do it without using any loop / recursion?

With the loop, it was an easy one, as we will divide n/3 till n%3 == 0

public boolean isPowerOfThree(int n) {
if(n<1)
return false;

while(n%3 == 0){
n/=3;
}
return n==1;
}

For solving it without loop we can simply do this

return (Math.log10(n) / Math.log10(3)) % 1 == 0;

n = 3

we can add log on both side log n = log 3 ᶦ

it will become i= log(n)/log(3)

After this I read book MySQL Internals

And finally, I was done for the day.

The trouble is you think you have time

This is one thing I have realized now, I am running out of time, and a lot more things are pending to do, a lot more things to learn and time is less, so I have to speed up my learning process.

Happy Learning!!!

--

--

Navneet Ojha
Navneet Ojha

Written by Navneet Ojha

I am Indian by birth, Punjabi by destiny. Humanity is my religion. Love to eat, travel, read books and my million dreams keep me alive.

No responses yet