Reverse a String - Java Interview Coding Challenge #3 [Java Brains]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we are going to be tattling fairly typical and very easy interview question in Java which was surprising you very commonly asked which is how do you reverse a string in Java it's simple but then it also uncovers some understanding about how strings work in Java some of the api's and all that stuff so I think it's a good question to ask anyway so in this video we're going to be discovering some of the ways in which you can reverse a string in Java there are two approaches that I want to tackle the first approach is using something called string builder and using the reverse api of it so Java has a reverse API for Strings and you can do that using the string builder that could answer the question but that may not be what the interviewer wants maybe the interviewer wants you to reverse the string manually and not use an existing API so that's gonna be the second approach that we're gonna tackle when you're asked this in an interview what I'd recommend you do is ask the interviewer if using a string builder reverse API is okay if they say no they are expecting you to do it manually then you go with the second approach but at least asking that question is gonna let them know that you are aware of these two different ways of doing this so we're gonna start with some writing some pseudocode about using the string builder to reverse using the reverse api and then manually reversing the string ourselves if you are doing this in a real world environment that's your on the job and you need to reverse a string so it's very very very likely that you're gonna be doing the string builder approach which is the first approach because there is an API already available the second approach is mostly for interview purposes all right so let's take into the studio code so let's say you have a string here which is STR equals let's say hello now you can do this using the string builder B or you can do this manually if you do get the string builder way you would use the string builder class to create an instance out of the string so you're gonna get a new string builder out of the string that's passed to you so string builder is an API that's provided in which lets you manipulate strings without having to create new instances of it you see the thing with strings in Java is that they are immutable immutable means when you make a change to an instance you're not changing the instance in place you're actually creating a new instance but the change is applied in the old instance is essentially ignored or held on as a different reference but basically when you make a change you never make a change in place you always get a new instance out of it so then you have a string you want to make like three changes to it right so you want to match this character or remove this character whatever else you want to do so you essentially have resulted in three new instances of that string and you just discard the old ones if you don't want to use it so the more operations you perform on a string the more instances you are ending up with as a result now this can be inefficient if you're making a lot of operations that's where string builder comes in and also another class code string buffer what these classes do is they kind of provide you with like a playground where you can convert a string instance to a stringbuilder instance and then make all your changes on that same instance and then when you're done and you're happy with the with the value of the string then you create a string value out of it right so you're operating on a string builder instance or not on the string instance so unlike a string instance a string builder instance is mutable so you can perform like hundred operations on the same instance and when you're done you say ok now I'm done with all my operations give me a string out of it and then you get one string instance which is more efficient as you might guess so this what we gonna do we're gonna take the string that we want to reverse and create a string builder instance out of it now the advantage of doing this in our particular case is that the string builder instance has certain API is one of the API being reversed so now that I have a string builder instance out of it I'm going to call the dot reverse method and then I'm going to do two string and now I have the reversed string but then if I were to do it manually things would be a little bit different so let's say I have the string here alright now I have a bunch of characters if I were to do things manually what I would need to do is loop through this from this side to this side and then get each character out and then put it into another string alright so this one goes here this one goes here so you're gonna be going from back to front and then inserting characters from front to back alright so this is one way to do it but now here's a problem every time you insert something into a string remember I told you about string being immutable you're essentially resulting in a new string here so every time you insert a character you get a new string which we don't want to do so instead of inserting to a string you insert to a you guessed it string builder a string builder here and then insert in reverse order then once you're done with all your inserts you're gonna get a string out of it which is the final reverse string so these are the two approaches that we're gonna be implementing let's see in code how that looks like so I have this reverse string class which is going to contain a main method this is going to be a launching point for calling these two I'm gonna create two methods for these two approaches so this main method is going to cause those two method so I'm gonna create a string hello world and then I'm going to call the first method which is I'm gonna title the reverse with string builder all right so it's gonna take a string as an input argument and then it's gonna return the reversed string so I need to write this method so let's say I write this definition reverse with string builder it's about taking a string and then it's been ever done the reversed string so what do I need to do here I need to first create that string builder instance out of this string which is what I'm going to use to manipulate the string and then all the string builder instance I'm going to call a reverse method that's the advantage of using string builder in our case it is going to reverse the contents of that string builder whatever string it contains and then when I do a dot to string out of this it is going to be the reverse string and that's it this is all we have to do to get the string to be reversed it's very simple alright now let's look at the other approach which is the manual approach so let's say I'm going to create a reverse manually method where not gonna call the reverse method of stringbuilder but I'm still calling the stringbuilder do manipulate the string so here's gonna be the signature off the reverse manually method it's gonna take in a string instance and then it's going to return the reversed string what am I going to do first first I'm going to get a stringbuilder instance to hold the reversed strength now I don't need to convert the existing string to a stringbuilder instance because I'm just going to be reading characters off of it I'm not manipulating the input string so I don't need a string builder for that but I need a string builder for the result string which is going to contain the letters in reversed order and I don't want to create a new string every time I insert a new character so I'm gonna create a string builder to hold that reversed string now I'm going to loop from the length of the string minus one so it's gonna go from the last point all the way to the beginning where I equals 0 or I is until I equals 0 so it's best to include I equals 0 as well I'm gonna do I - - look at all the characters from the back and I'm gonna get the character by using STR dot k hat carrot head is going to take the index which is I and it's gonna give me the character of the string at that particular point I'm gonna take that and then append it to the string builder are we gonna be using the append method of string builder which lets you append a character or a string to it and this is gonna be helpful because we're gonna be appending characters one by one and then it's gonna go into the same string builder instance now once it's done once I have looked through all the characters from end to beginning then I put it into that string builder instance I'm going to the two string method again I'll have the string builder the robot string value I need to make a string out of it and then return it back so I'm gonna do that and then we are done with the reverse manually method as well so we have these two methods which is going to return the reverse of a low wall but two different approaches both of them using string builder alright so I'm going to switch to my Eclipse window which has the exact same code that we just saw and we have two calls let's run this and make sure they reverse the string properly and they do so this is how you would reverse a string in an interview either of these approaches if you're doing in a real-world scenario again like I said you would just use the string builders reverse method there could be another twist to this question where the interviewer would ask don't even use string builder right how do you do this without even using a string builder you would still go with the second approach which is reversing it manually but instead of using a string builder to append the strings you would just append strings directly right so it will be like string blustering plus strings we are adding characters one by one the disadvantage like I mentioned is that you're creating new string instances every time so it's gonna result in a lot of objects being garbage collected by Java at some point so if the interviewer mentions this caveat and then asks you to implement some things without using the string builder you can just append strings but then make sure to let them know that this is horribly inefficient because your resulting in so many ways to instances [Music]
Info
Channel: Java Brains
Views: 84,942
Rating: 4.8974357 out of 5
Keywords: java brains, java, interview, coding, coding challenge, java interview, java interview questions, interview coding challenge, interview practice, koushik, koushik kothagal, java interview videos, java interview challenge videos, brains, kothagal, kaushik, tutorial, programming, eclipse, beginner, challenge, java programming, java tutorial for beginners, training, reverse, string, reverse string, stringbuilder
Id: gy1uve2BwG8
Channel Id: undefined
Length: 9min 50sec (590 seconds)
Published: Sun Jul 28 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.