Reverse an Integer value - LeetCode Interview Coding Challenge [Java Brains]

preview_player
Показать описание
Interview Question: Reverse an integer in Java

Difficulty level: Easy
Language: Java

Welcome to the Java Brains Java Interview challenge series of videos where we tackle various coding challenges ranging from the beginner level to advanced level - especially in the context of coding interviews.

Any coding problems you would like me to explore? Please let me know in the comments what you think!

#java #interview #javabrains
Рекомендации по теме
Комментарии
Автор

I did the same but my code was way longer. Your solution is the best. Just add "
return reversed;"
after while loop ends

DK-scvn
Автор

I got this challenge in an interview, I have just converted the integer to string, I reverse the string and convert back to a number :)

nabilelhaouari
Автор

very enjoyable video. Having moments like this reminds me that intellectual satisfaction is highest form of pleasure. thank you sir.

RameenFallschirmjager
Автор

"exceptions are for exceptional situations" - I'm stealing that line. :)

sirjonaz
Автор

Wow this is the first video I have seen from your channel and it is amazing! You go in deepth and make it so understandable, and then later implement the code! love it!!!!

fufuisgood
Автор

That was awesome!!!

Thank you very much Koushik, today I learned something new, really appreciate!

dkryeziu
Автор

What about this proposition :
public Integer reverse(Integer myInt){
String s = ""+myInt;
String s2 = "";

for(int i=0;i<s.length();i++){

}

return Integer.valueOf(s2);
}

HammamMounir
Автор

Two things to mention here
1. The code did handle the exception cases of integer range boundaries BUT didn't return the "reversed" value outside the loop to handle the happy case
2. I understand that A smaller number in the range of integer might possible cross the integer range while reversing ( Reverse of 12345 is 54321 ) which is bigger than original . Is the min range meant to handle the negative integer numbers ?

sharathchandrareddy
Автор

I think the sign of the integer being reversed needs to be handled outside the reversal. So one has to take the absolute value of the number, reverse that absolute value, and then multiply the reversed number with 1 or -1 depending on the sign of the original number. At least in Python, the remainders of -ve numbers do not work in a sign-agnostic way.

meeradad
Автор

We appreciate you ..!!! keep solving more leetcode problems..

thanga
Автор

These videos are easy to follow and understand. Thanks, Koushik!

diegoguzman
Автор

great tutorial....pls more of problem coding for interviews

sagarmeena
Автор

int a = 54321;
StringBuffer str = new StringBuffer( ""+a );
System.out.println( str.reverse().toString() );

In this solution, Need to check if it's +ve or -ve and add the sign later on accordingly.
This is shorter but requires parsing int to string which is less efficient, but it does the job for lazy people like me. I'm sure the client won't mind waiting 1 extra second for his reversed number.

ShinAkuma
Автор

I think while condition should be replaced with while (input > 9) and after while loop 1 more statement reversed = reversed * 10 + input; for last digit. It will work for input < 10.

abhijitmadchetti
Автор

I wonder if I can use an array to inverse this array.

Aaron-yuzo
Автор

I think, this could also be achieved by reversing string. String str="54321";
int

rushikeshr
Автор

Mayby it will be simplier?
StringBuilder reversedNumber = new
return

antonslonkin
Автор

This problem is also a good candidate for recursion as we are decreasing the input at every iteration

private static long reverse(int input) {
if (input <= 0) {
return reversed;
}
reversed = reversed * 10 + input % 10;
reverse(input / 10);

return reversed;
}

msk
Автор

Thank u very much sir for leetcode keep solving more problems

parshuramrv
Автор

Your videos have been so helpful and clear! You rock

ivanviveros