Dev HUMILIATED after writing Brute Force Search on a Sorted List

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
speaking of which did you write that a pointer on at the beginning element sorted list get the value at the pointer does it equal this element which I don't think it's declared anywhere and does it not equal to that element if it doesn't then um make sure that the length that the index is still in bounds and well increment the index before you check that and if it's still in bounds then you continue to the next iteration of the loop and this is not the most readable code because there's nothing inside the loop so it's just the condition and if it ever terminates we will return and if the index is in bounds we'll return the index and if it's not then we return Nega -1 as the default so obviously this isn't readable code but there's something else wrong with it uh given and I've seen this episode before Oh and sorted lze is greater than plus plus index you a boot Force search and assed well look guys it was my first day and I was nervous and I was hurrying so really that's why you decided to write the slowest sub routine in history just for the record this is what happens in coding interviews if you ever make a mistake this is what's going to happen anyone could have made that mistake yeah it's true in fact I could ask for a show of hands but instead of doing that uh I'll just uh could you have made that mistake could you have made that mistake you have made that mistake made that mistake the joke is that he's doing a for search of the room yeah I know mistake then why did you do it oh my God I hate this guy so much and the worst thing is when you're a kid there's always a guy like this and you think when you grow up they're GNA go away but no it's like when you go to like visit your family and they're like oh so you graduated where are you working now and you're like oh well actually I don't have a job right now and they're like well what do you mean I thought you graduated right and they're just like oh my God you have made that mistake you have made that mistake and it looks to me like this is probably Java because when you look here Java is one of the only languages where you need a method to actually get a value from an array you know who needs brackets right just so you guys know this is actually a leak code problem I think it's leak code 704 and if we go to the neat code 150 and you go to the binary search section you can see it's actually the first question in the list and it looks like leak code is down and so just to turn up the difficulty let's actually do this in Java so uh we initialize our pointers at the end point right is that I'm pretty sure the length of an array you can get like this in Java and by the way they weren't using an actual array in that problem they were I think uh using an array list but still you should be able to you don't you shouldn't need a method for that you don't need a method in most languages for that so the way I do this I already know my template I have it in mind I've done this so many times that I always put the return in the else so if we find the target we return if we don't we'll do it out here and so now I just need the conditions if the target is greater than the middle value then we search on the right side uh is going to be mid + one otherwise we do the opposite and the condition is also going to be the opposite so let's run that and we got it never run linear search on a sorted array because you can do binary search this is Elite code easy fellas
Info
Channel: NeetCodeIO
Views: 73,128
Rating: undefined out of 5
Keywords:
Id: Y6Lo38YPWjY
Channel Id: undefined
Length: 3min 49sec (229 seconds)
Published: Tue Feb 13 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.