Java Interview Bootcamp from LeetCode.com - Part 1 | Saran Siri | Skillshare

Java Interview Bootcamp from LeetCode.com - Part 1

Saran Siri, Instructor

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
6 Lessons (24m)
    • 1. (709) To Lower Case

      3:38
    • 2. (389) Find the Difference

      4:03
    • 3. (700) Search in a Binary Search Tree

      4:11
    • 4. (832) Flipping an Image

      2:54
    • 5. (643) Island Perimeter

      5:02
    • 6. (1046) Last Stone Weight

      4:22

About This Class

You have learned Java and want to improve your coding skills, or have an interview coming up? Let's try real Java problems from LeetCode.com with me!

In this class, you will be working on leetcode.com problems, first by yourself, and then, you can check out my approach on that problem. Some of these problems are collected from Amazon, Google and Apple interviews.

Disclaimer: What I wrote in this class is my first take when I encountered the problems. It might not be well-optimized as the goal is to get 100% correct.

Transcripts

1. (709) To Lower Case: Hi, everyone. This is gonna be my new Siri's to basically ensure you how I could using Java by practicing on the interview problems as always. If you want to try it first, such for this title. Just post the video, do it and then you can come back to see my take on it. Let's get started. The problem. We're gonna do induced video. Today it's number 709 to local case, so it stays improvement function to lower case that has a string parameter str and returns to the same string in lower case, for example Hello with the capital edge, the awkward should be the same string in lower case. Same for here and same fall. Leslie, if you're using Java, you might know that in the string class it has to lower chest already. So maybe let's try it out first. So this return str doctor to lower kids And there you go. It does work credit summit, it causes. But I think what did the question wants is to implement your own to lower just method. To do that, we have to first separate East collector in a string, so let's try, go through each character and prayed offers to get d corrector We use Charente and let's return nothing for now you should print Hello. Next thing is to detect with ah Distracter. Easy up a case or not. To do that we have to understand the asking table which I have it right here. We can also see the character in the number of representation. That means capital a equals 65 cabbage Josie Eco's 90. If the character is in between this range, we can then add 32 to it. So the upper case A we become lower case A So we should check whether the character is in between this range. Maybe we could start inside variable See, then asked If sees between 65 90 it is true. Then we have 32 into it so we might have another variable to start each corrector. I gotta call the result story is one. In result, if the character see is a lower case, it just doesn't go through the east statement. So it stays the same scene. Then finally read return the result right out it's the same thing and that's it For the first video. Thanks for wishing 2. (389) Find the Difference: Now, let's try on this problem. Problem number 389 Find a difference on easy Given two strings S and T, which consists off only lower calculators String T east generated by random shuffling string s And then at one more letter at the random position find the letter that was added in T, for example. We have a B c. D. He might not be a BCG. He could be a CBD. And then it asks one extra collector, which is e. It could be at the beginning and we have to detect that corrector and return it out. As always, you want to try it closet and then come back. Let's get started so we could convert s and t into two aerialists off correct er's And then cycle through is if it contends the same corrector. Then we removed that corrector in t so because to be removed me go to see is it content? See? Remote. See? It's going to be real. At the end of the follow, it would only have one item left, which is the corrector that we want to return it because we used content Smith that the order inside the aerialist t doesn't matter. So first of all, we have to convert both S and T into analysts. So I'm gonna create a list of corrector cord as list and we were using our list. Go through each collector inside as dot to Charlotte. This will convert s into Esha array and psycho through each item story inside variable see , and then resent. That is one into s n'est and also a creative list. Then we have to go through each character and then remove the corrector. Inti lists it is found in s rest Since recycled through a list off corrector we have to use corrected as an object corrector issue is in as list. Check whether each s this inside t list. Then we remove it from the list. So at the end, we will end up with only one item inside T list. So return it out. Do you just not get the 1st 1 which is correct, but it's better to just see how it works. So I'm gonna print the T list to see every time a corrector inside it get removed from the list. So as you can see a on the first loop it got removed from the aerialist and then be see and Indy. So we end up with key and Religion T trying to run it. We got 678 minutes again because we print something out and it takes more time, so it's better to just come in everything out, I don't think. 3. (700) Search in a Binary Search Tree: Now let's try this problem Number 700. Search in a banner research tree on Easy. So in order to do this problem, you might have to understand what its battery tree and by the research tree first, given the root note off a binder resource tree and a value that means this tree and of value Here, you need to find a note in the Bannon Resource tree that the nose value Eco's a given value , trying to search for two and that is to return this up tree rooted with that note. It's session on doesn't assist return now, so you go home for you. Go down to two and then cut the branch here and then return the whole sub tree. And if the searches three you just have to reach and three without any left. All right, note. As always, if you want to try to now go to lead coder, come and come back to see my take on it. Just get started. So in this case, we have to use a requisite method, meaning that decision BST you call itself so let's deal with the cases first, when the tree that we come through doesn't exist. We just returned now and then It is not equal to know we can then ask me the value of this tree because the validated we want to find it is through. Then return this route. If it's not, then we could start asking if it has a less not if the left nor does exist. Then we would call itself and put the left note as the beginning note. But in you work from four. And because four has the left note it goes to two and goes to one because one doesn't have left right now, it will end up in now. For example, if the searches 10 if it goes to one and one doesn't have left all right, now then it should return now and we will say, go to the left Notes and also even has a right note. We can search 42. Tried it out. Seems to be working, trying to summit and we have an error. The problem is, in some cases, the right note. One gonna want it all. If you want to find three, it goes to four. Four has left note which is to and then it goes to left note again, which is one. And because one doesn't have any left or right note, it returns now returns back to one who cause it without even go to the right note. So we have to cast that before it goes to the roof and trying to shake on the right note to so in here re central and result note and cast the result off the left note here. And then we ask if this result from the left known is now. If the result appears on one of the left note, then we can return it. It is not. You should not return anything yet, and it should go to the right note and it works. That's how you do it. 4. (832) Flipping an Image: problem number 8 32 Flipping an image giving battery metrics A We want to pretty image horizontally, then inverted and returned a result. You, Mitch, Do you believe in the mess hall recently being stepped, this row of the image is reversed, for example, for 110 all centrally resource in their 11 to in Vernon image means that each zero is replaced by one is one of the best by zero, for example, Block. Then we have the example here. So we have 1101 goes here. Zero goes there. Same as this. Flip it mean one goes here and one goes there. And then we inverted So 0 to 1120 as always. If you want to try it, go to lead co dot com, tried it out and come back to see my take on it. So this problem I would create two D array to start the result first, take the first dimension in the second dimension from a then using tuf alludes to access the second dimension tight. Um, plus, you want to get the value from a in this case one and we want to start at the last position . In result, we want to get from second position and started. I had the second to last position. The better way to reach its to get from this position. Put it in here. So from a I would get the value backwards. So we get from a I the science and menace one made us day. Now if you want to start it, Weaken started in position. I j He is where we flip the image whole centrally. But not only that, we need to invert it first. So before this, we have to invert the value inside from a variable asking with some A is equal to one. It is true then from a equals 20 Otherwise it goes to one and then store it finally be returned a result 5. (643) Island Perimeter: number 463 on Easy Island perimeter. You're given a map in from off the two dimensional in digit grid where one represents land and zero represents water. Rig cells are connected horizontally and vertically, but not diagonally. The great is completely surrounded by water and there is exactly one island. The island doesn't have legs. Did you mean the perimeter of the island? So in this case, the perimeter is the yellow stripes in the image below. 123456789 10 11 12 13 14 15 and 16. Once again, you can try to sell first. Just search number 463 on lee cho dot com And here is my approach. So if you're stand on this position y zero x one on the left side is the water so the adjacent as much. If it contained zero, you could count that as a one and also if it connects to the issue of the grid. So if I moved to this position my topside Israel, the rights I zero the bottom side is zero. So I only count three zeros. So I'm going to create a new method. Coach Walk walk roussanne at. Why exposition? And you gotta count how many times he adjust themselves with zeros or outside. Agreed. This one is when we can't walk to the left side. That happens when x iss at zero already. That means right here you cannot walk to the left side or when you're at here. And the left side is zero. I don't have to add another condition with Ex Menace. One is greater than zero because that's the opposite. Off X equals equals zero. Whether the left zero or u at the h. Then be increased the counter. Same thing for the right side whether we're at the edge of the grid. But the value of the right grid is zero. We're at here. We cannot walk beyond that. Why you go zero or the top grade is zero. Same thing for the bottom. Oh, I forgot that. Sorry. Then we returned account. Now if I planned a number in, for example, why is 11 which is this grid? It would return zero. We're gonna cycle through every grade. We only have to go to a great that contains a value of one. So I'm gonna ask if it is one. Then I'm gonna walk and we should have another variable to click. He's come from the work method and return it. Yes, the era. Try this a minute. Nice. 6. (1046) Last Stone Weight: problem Number 10 46. Nice stone. Wait. We have a collection of rocks. Each rock has a positive in. Did you wait Eastern issues to habeas rocks and smashed in together? Suppose the stones have weight. X and y blah, blah, blah there. See the example. So we have six rocks eastern. We picked the two heaviest rocks. Smash them together h metal seven, which is one and then started back into the array. Then we peaked, too. Heaviest rocks again. Smash them up. Four minutes to is to put him back to in one two minutes. One is one. Put them back. One minutes 10 Don't have to put them back and we end up with only one as always. If you want to try it out first go to lead co dot com Search 10 46 dude by your own first, then come back to see my approach. Let's get started. That's about all we have to get rid off all the edge cases, which is when we don't have any stone. And when we only have one stone, it could be five and then we return five right away. So is there is no stone return zero if it contains one stone returned their stone then, because we have to pick two heaviest rocks every time after we smash them is better to put our stone inside a realist so that we can use collections there sort to sort them out. So first convert array to aerialist. I will name it s list for in stone inside storms added into s list. We're gonna use do a while. So as long as we can smash them, keep running inside the loop. Why? A list is greater than one. Because if it only has one, then we can get out and returned the one that is left. And that is the only one in science wrist now inside to do while first we have to solve them out. We're gonna have to import job you two collections and call collections their sword s wrist and specify that I want to start it in reverse order. So the first look attempts in the list should be a 72111 Then we take the first heavier stone and story somewhere. Pick it from the first element and story in stone one, then Tim Billary remove it from the list. Take the second heaviest Iraq started in the stone to and also remove the first position because it's moved, then smash it. And if the new stone has weighed more than zero, we can add them back in the list. Last but not least, sometimes you could end. I'm with two and two. If this mashed together, the remain would be zero and we don't have any element left in a list to prevent that. We could have another condition here saying that if there's no element left, just return zero, does it?