🚀 neetcode.io/ - A better way to prepare for Coding Interviews 🧑💼 LinkedIn: / navdeep-singh-3aaa14161 🐦 Twitter: / neetcode1 ⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -... #neetcode #leetcode #python
Пікірлер: 326
@NeetCodeIO7 ай бұрын
Kinda surprised how many people think it's a Linked List.. does this look familiar: List list = new ArrayList();
@rinat21607 ай бұрын
If you are writing a library which functions work reasonably well __only__ when Lists they accepting are ArrayLists, you are writing a bad library. If a function requires constant-time random iterators, it should only accept containers which have constant-time random iterators. That way compiler will prevent library users from using your library incorrectly.
@IronicHavocАй бұрын
@rinat2160 its a leetcode question though, not a library
@IronicHavocАй бұрын
@rinat2160 Also what do you mean by "random" iterator? Do you mean, like, arbitrary/generic?
@KeepOnBlazingАй бұрын
@@IronicHavocrandom means random access also knows as constant access, like arrays. In linked lists, on the other hand, to get the n-th element you have to pass through all the ones before it (because of how a linked list is implemented, look it up on wikipedia) and thus the access time cannot be constant i.e. random
@LeeO070429 күн бұрын
why is he calling variable "pointers"?
@taaaaaaay7 ай бұрын
Bro was able to identify the Leetcode number from the top of his head💀
@chillsjiujitsu7 ай бұрын
That’s why he’s the GOAT
@aflous7 ай бұрын
His name is Neetcode, what did you expect?
@VampiricBard7 ай бұрын
@@aflous 🤣
@xcuu7 ай бұрын
Tell me you’re a sex haver without telling me you’re a sex haver
@kingpen58667 ай бұрын
Bro
@LandoKalrissian7 ай бұрын
For small arrays, linear search actually beats binary search because it's so branch-prediction friendly.
@NeetCodeIO7 ай бұрын
Also why didnt he just do .indexOf(element)
@LandoKalrissian7 ай бұрын
@@NeetCodeIO I usually code in languages that don't have that method built-in, so if I were tasked with writing linear search in Java, it wouldn't occur to me to see if a built-in function with such functionality exists.
@CrabGuyy7 ай бұрын
@@LandoKalrissianhe was being sarcastic, you stated the obvious missing the point of the video, so he prompted you with a equally useless comment. No hate, just explaining
@ecchioni7 ай бұрын
Same reason why C# uses quicksort and insertion sort combo as its default sort.
@TacoMaster077 ай бұрын
@@CrabGuyy cache locality and branch predictability effects on different DS and algorithm ordinance is obvious? Dumbass.
@halfjainam7 ай бұрын
did he just fucking quote a LEETCODE question with NUMBER by looking 4 lines of code!!! 😭😭
@someoneontheweb45157 ай бұрын
its not that big of a deal lmao
@rockdude11227 ай бұрын
@@someoneontheweb4515 most people watching this video or just programmers in general have not done enough leetcode problems to remember the exact leetcode problem by just looking at 4 lines of code so its pretty impressive.
@Random-ev6xq2 ай бұрын
bro im sayin i thought i was tweakin 😂😂😂
@monkemode812817 күн бұрын
@@rockdude1122 Yeah I have 300 problems solved on leetcode over my time in uni but I don't remember any by number (except the first few).
@GnomeEU7 ай бұрын
When do you ever use binary search in a real project? It's either dictionary lookup or flat list. 99% of our performance issues are crappy sql queries, a bad coded UI framework, or just plain logic errors. If binary search is your problem then it looks like your project is in an AMAZING state!
@JosephGleespen7 ай бұрын
It's a leetcode problem bro, that's what they are like
@mycommentmyopinion7 ай бұрын
@@JosephGleespen Literally not the point.
@NeetCodeIO7 ай бұрын
i agree, even if you needed binary search you would use the built in one. Even i made a mistake in mine (l + r can lead to overflow). Best to rely on std libraries, even in the video why didnt he just use the built in linear scan of ArrayLists.
@QuanNguyen-og6pq7 ай бұрын
yup. Bad sql, bad joins has caused more slowness than any search problems I've seen.
@talareese5807 ай бұрын
I had to use binary search for a procedural level generation component in my game. I had to randomly choose an asset to spawn on a grid. But each asset had an associated percentage chance of spawning. So yeah.
@shobhandash87207 ай бұрын
the bully: good, now do this in O(1)
@sharpfang7 ай бұрын
ret=-1; for (i=0; i
@ZattilioАй бұрын
Ok i will use a hashmap
@thepriestofvaranasi25 күн бұрын
Proceeds to create a hashmap 🗿🗿
@LukeDrumm7 ай бұрын
Discussion of performance without mentioning CPU caching is ... interesting.
@elijahbuscho77157 ай бұрын
This is why I use PHP and throw performance out the window
@niggacockball79957 ай бұрын
@@elijahbuscho7715 unironically based, only old hags with shitpads from 2001 complain about performance like bruv just get a new pc its not that hard
@TehIdiotOne7 ай бұрын
@@elijahbuscho7715 I do Python
@naughtiousmaximus7853Ай бұрын
@@elijahbuscho7715 No need to solve a problem if there isnt one,
@JNelson_4 күн бұрын
I actually work on a project which requires hundreds of lookup tables and for lookup tables with fewer than 100 elements brute force (because of caching) is as fast as naive binary search and faster at lower numbers. That said spending a lot of time optimising it, you can write a branchless variant and you can get the compile to unroll your look if you know the size at compile time to give you log2(N) iterations. This beats out naive binary search and brute force by only about 2 times. It's all about measurement and use case and seeing if it's actually worth the time saved.
@444limm7 ай бұрын
there are actually quite a few languages that better to use method to get an element with index. Because with [ ] most programming languages will just panic / throw exception when index out of bound, but with method like .get() we can make the return type not the bare element but a wrapper like Option in rust so Some(value) if the value is present and None if the index is out of bound
@nikhilchouhan18022 ай бұрын
or just use std::optional
@nikhilchouhan18022 ай бұрын
in c++ ofc
@justADeni7 ай бұрын
2:30 No, in Java the syntax to get a value from an Array (as you said) is array[index]. However to get value from a List, you'd use .get(index) as you can see in the vid.
@LeeO070429 күн бұрын
why is he calling variable "pointers"?
@monkemode812817 күн бұрын
@@LeeO0704 It's not a pointer in the sense that it points to a memory address, but the "point" of the variable `i` in the bad code is to "point" at index in the array.
@EvanPilb8 күн бұрын
2:00 bro it's your twin
@n_x18917 ай бұрын
If I was getting grilled like that I would have just said that in spite of the list saying it was sorted there was no guarantee so I had to check manually. Might work 😂
@likesonggameАй бұрын
Yes, this is true. I got scream for this in my first year of work. Even thought the guy who build and manage the database should have sorted it but they argue that people never do their job correctly so i have to check it again. Sadly coders never checked or test their programs because of laziness so yeah it's a good measure.
@monkemode812817 күн бұрын
I'm front end so I know my code will never run over thousands or millions of elements, a lot of times I just sort stuff like responses to API calls which say they should come sorted because I've had cases where they aren't or they aren't sorted in the way advertised. The cost is low for so few elements and it adds some resilience against bad updates to the API implementation or weird responses.
@jibreelkeddo70307 күн бұрын
@@monkemode8128 The downside of doing that is that you end up covering up what should be a bug report to the backend guy.
@NeetCodeIO7 ай бұрын
I think the more embarrassing thing is the person using a Windows laptop at 1:18
@NeetCodeIO7 ай бұрын
I'm tempted to delete your comment and edit mine...
@abdalla44257 ай бұрын
LMAOO @@NeetCodeIO
@pengie_7 ай бұрын
I run that same laptop but it's got linux running on it, had to put a sticker of my distro on it so people didnt think i run windows
@jonathanng71417 ай бұрын
What is wrong with Microsoft surface laptop?
@GreatTaiwanАй бұрын
@@pengie_expensive, poor performance per watt and per dollar Why would u buy surface (beside maybe Lilith the package itself that hardware removing the touch screen and so on)
@kebien602010 күн бұрын
Always remember though: Binary search produces maximally unpredictable branches (bad for speculative execution), and jumping around in memory (bad for cache locality). So it's actually slower than linear search for small lists. The exact element count where binary search becomes faster than linear depends on multiple factors, such as the list implementation, the element size, the size of the processor's caches, etc. So you have to benchmark in order to actually know for a particular situation. It's often suprising how big the list has to be for binary search to be faster.
@JNelson_4 күн бұрын
You can actually write a branchless binary search. I work on a project which requires hundreds of lookup tables and for lookup tables with fewer than 100 elements brute force (because of cpu cache) is as fast as naive binary search and faster at lower numbers. That said spending a lot of time optimising it, you can write a branchless variant and you can get the compile to unroll your look if you know the size at compile time to give you log2(N) iterations. This beats out naive binary search and brute force by only about 2 times. It's all about measurement and use case and seeing if it's actually worth the time saved. Saying to always use binary search like this guy is saying is brainless.
@nemikuuro7 ай бұрын
There also seems to be a bug in the solution from the clip - it always tries to get the first element from the sortedList? What if the list is empty?
@romanivanov61837 ай бұрын
Hey! l +r ?? You will get integer overflow!
@NeetCodeIO7 ай бұрын
🤦i've dishonored my entire family
@oussamabennasr49367 ай бұрын
m = l + (r-l)/2
@blasttrash7 ай бұрын
Correct me if I am wrong, but this issue doesnt exist in python right? Only in C, Java, C++ I guess coz of how ints are handled? If thats the case, it might explain why a python dev would not think of this. Also in Java, we have to be careful while writing comparators with integers(especially with heap based questions like top k etc). If we do a - b instead of Integer.compare(a, b) we will have similar issues.
@se4geniuses7 ай бұрын
@romanivanov True, but if you're iterating over more than 2^((sizeof(int) * 8) - 1) / 2 elements, you have bigger concerns to deal with.
@TehIdiotOne7 ай бұрын
@@se4geniuses You do, but it's not entirely unheard of
@fromjavatohaskell9097 ай бұрын
3:44 (l + r)/2 would cause overflow for big arrays
@tylerd5924Ай бұрын
🤓☝🏻
@jamjam3448Ай бұрын
Yeah l+(r-l) /2 is better
@ursalight21 күн бұрын
@@calculovo4219 oh right
@yashmalviya014920 күн бұрын
(l + ((r - l) >> 1)) 😎
@monkemode812817 күн бұрын
What about....... l/2 + r/2 Seems intuitive to me.
@SuperMaDBrothers7 ай бұрын
it's not a mistake, it's being frugal with your time when the complexity doesnt affect the runtime for fuck. if he's searching a list of size 3 then the complexity doesnt matter. this tv show fucking sucks, no one acts like a software dev. painful to watch
@234lk7 ай бұрын
Do you really think anyone would watch the show if they acted like software engineers?
@SuperMaDBrothers7 ай бұрын
@@234lk you really wanna watch a bunch of people act like brainless zombies? I guess that's what modern tv has become, let me just watch my character act in the way i want them to act. That is SO what jim and pam would do! LOVE ittttt!
@mycommentmyopinion7 ай бұрын
I doubt it's made to be realistic lol, nobody would want to watch a realistic version
@anon_y_mousse7 ай бұрын
@@SuperMaDBrothers No, it's definitely a mistake, because there is a function to perform the search for you on a sorted array in every major programming language, even Java.
@meatbleedАй бұрын
hey buddy software dev is boring as fuck. clearly they're gonna make up more interesting scenarios than just sitting there coding SQL or some lame shit
@unixpro19707 ай бұрын
Silicon Valley was a popular TV show, but this is ridiculous and uncalled for. He should have stood up for himself and left, if they continued to act like this. Constructive criticism is one thing, but abuse should never be tolerated. They acted like a bunch of clowns 🤡
@asandax67 ай бұрын
Not gonna lie when I write code I always write whatever pops in my head. I optmise it latter if I need to. The reason I do this is because I sometimes get side tracked into optimizing a single piece of code and by the time I realize I have wasted a lot of time on it. So better make it work then improve once it works.
@amzkerАй бұрын
Yes, exactly
@ramanchaudhary251819 күн бұрын
Similar thing with leetcode problems if you try for the optimal solution first you will go in circles ,if you brute force it then you find out how to actually implement a optimisation and why it is needed
@taragnor17 күн бұрын
Yeah, I generally use this practice too. Unless I know the list is going to be massive, or I'm specifically coding something that needs top end performance, I generally don't even bother with any optimization and do the quick and dirty method. This is the kind of code you can always refactor later if need be. Sometimes you may even screw yourself being too clever. Like if the sortedList was actually a linked list and not an array, doing a binary search would often get worse performance than a linear one.
@mukeshrawat13047 ай бұрын
Guess what?! I landed on Neetcode's YT channel while doing a brute force linear search on KZbin. Definitely worth it ❤😊
@ofbaran7 ай бұрын
I see myself using linear search under pressure, it just seems like the most natural way of searching in non computed primitive way of searching items in real life, of course in a real situation the best method would be using a built in search mechanism of the language such as the .indexOf() or its equivalent if present in the language and let the compiler or the interpreter get clever with it if not implement a more sophisticated searching algorithm more fitting for the expected data size. For me the problem in the video is not the error itself but rather the reaction to the errors. We all make mistakes, especially in the beginning but instead of putting each other down over it we should be correcting each other while lifting each other up. Unfortunately I have seen this kind of snobbery in real life. Pressuring devs into making untested solutions that even they couldn't revise themselves to get solutions and then talk them down for mistakes.
@ofbaran7 ай бұрын
Note: apparently I was wrong, Java implementation of .indexOf() is also linear search! In large samples a binary on a sorted array would be faster
@ofbaran7 ай бұрын
The linear search is still useful if the array is unsorted, I'm sorry.
@ishanbajpai69407 ай бұрын
Silicon Valley, baby!!!
@vinayemani41057 ай бұрын
Well, to be pedantic, without knowing the type of sortedList (assumed in the show to be an array, hence the joke) which could've been a LinkedList, we cannot really comment on the efficiency of this code. Binary search on a linked list is worse than linear search.
@phoneix24886Ай бұрын
But it was really cool that a TV show brought up the basics of CS and actually showed a code that makes sense. Silicon valley is still the goat along with Mr. Robot among all tech based movies/series.
@thimowellner76868 күн бұрын
I'm not too familiar with the implementation of a LinkedList, but I assume that the given solution would still be terrible for it, as it uses .get(index). For a LinkedList this method would have linear runtime and it's used in a loop over all indexes, making the algorithm's runtime quadratic. If it's a linked list, the search should follow the pointers directly, not use a retrieval method that has to follow the pointers again and again.
@cwagnello22 сағат бұрын
@@thimowellner7686 yeah it's a linear scan for .get(index), assuming that it's a LinkedList. But saying that anyone using Java is going to use an ArrayList 99.99999% of the time so that wouldn't be an issue.
@CottidaeSEA10 күн бұрын
If you're ever going to do a linear search, please just use a good old for-loop. Don't try to do anything fancy.
@mycommentmyopinion7 ай бұрын
Fuccc I think I need a refresh on algorithms, I use them so rarely I keep forgetting how they work lol
@chandrap83917 ай бұрын
The current generation CPU / GPUs process what ever junk we write inside a loop in a matter of no time. For 90% of Application Software the current Processors just chew any loop throwed at them before we notice it unless we have millions of rows in memory, which is not the case in 90% of the work.
@connormc7117 ай бұрын
Wow. I have never seen someone with that close of an approximation to real skills
@TacoMaster077 ай бұрын
Java allows direct array access(I don't think any language doesn't since the purpose of an array is data storage in memory sequence), Java's arrays are implemented as objects, nums[5] = 69 is a valid assignment.
@jonathanng71417 ай бұрын
I just learnt sorting this week and this video was perfect in helping me understand how the code is implemented!
@kantamei77074 күн бұрын
Pretty sure there's a bug at 3:08. Because java doesnt use bignum, l+r can overflow if the array size is near int max.
7 ай бұрын
NitPick : In java you need a method to get a value from a list, not an array.
@technophile_7 ай бұрын
I think you forgot about the fact that (l+r)/2 might cause the integer to overflow. I know that solving the problem is not the point of the video. But I’m just putting it in here.
@mgancarzjrАй бұрын
In case anyone needs to see this: int mid = left + (right - left) / 2;
@monkemode812817 күн бұрын
For anyone wondering, to prevent this, you could use the distributive property of division and convert: (l+r)/2 to (l/2)+(r/2) edit not really
@steffenrumpel27847 күн бұрын
@@monkemode8128 Assume L=3 and R=5. What you get in the second case is 1+2=3 (because of integer based calculations) which is still within the bounds, but certainly not the "middle". mgancarzjr's "left+(right-left)/2" is better.
@monkemode81287 күн бұрын
@@steffenrumpel2784 Yup, you're correct :) Someone else pointed out my error, I forgot about integer division going down to floor. Good example it makes it very clear.
@dracsharp7 ай бұрын
From a business perspective and assuming you are building an MVP or a possible feature, the fastest to add solution is the right solution.
@xavier77697 ай бұрын
well bin search is fast to add he did it in like 2 mins
@mycommentmyopinion7 ай бұрын
@@xavier7769 With a bug that could cost money and time to debug :D
@def__init7 ай бұрын
Skill issue
@rinat21607 ай бұрын
Code in the show mentioned a list, not an array. You can't really do a binary search in a list unless you preprocess it first. But then its not just a list anymore. The joke was that the code in the snippet tries to get nth element from a list N times, so the overall complexity is O(N^2).
@AzidHouse7 ай бұрын
Why are you assuming that sortedList is just an array of integers? In Java, simple arrays don't have methods. You have to use [index] as usual to retrieve an item. On the other hand you cannot use < > operators for comparison between two arbitrary objects. And clearly sortedList is an ordered objects container. The proof is that here is used the equals method. So, what is the solution in that case? I'm truly interested, I saw many answers that address the problem without taking into consideration those aspects.
@ramanchaudhary251819 күн бұрын
The .size() method returns an integer value if that’s what you meant ?
@AzidHouse19 күн бұрын
No, I just want to see a general purpouse Object comparison with the use of the .equals() method and not assuming that the object is simply an integer comparable with angular brackets. Example: you must find an object that is inside the sortedList but ordered by more than one field. To be clear, assume that you have a Box object that you need to retrieve and his position in space is given by WarehouseNo, Lane, Rack, Shelf. The comparison logic, obviously, resides into the equals() method. You cannot use other means to compare 2 instances of the same object.
@imerence62902 ай бұрын
I'm actually impressed how short the code is.
@shardator5 күн бұрын
Linear search can be a lot faster than binary search if the elements are stored consecutively in memory, and the list is relatively small.
@lmao49827 ай бұрын
you don't need a method to access array members in java, it has c-style syntax for that. these are lists. edit: ah you just did it
@69k_gold7 ай бұрын
Wouldn't this be a massive code duplication issue? He's reinventing the wheel
@Dom-zy1qy7 ай бұрын
This dude writes binary search like I breathe.
@edwardmitchell65817 ай бұрын
It's basically his full time job.
@ceciljoel95777 ай бұрын
For seniors those algorithms for really that easy
@yt-sh2 ай бұрын
binary search is a pretty good algorithm, only problem is arrays have to be sorted first...
@hassaniq07772 ай бұрын
yea bro i hate this bruh I was gonna write Binary Search in C or C++ but then realized the list has to be sorted 😤 now I'm gonna implement both the sorting algorithm and the binary search
@DankMemes-xq2xm16 күн бұрын
@@hassaniq0777 the sorting algorithm is the hard part. Unless you're being asked to do it from scratch for homework or on an exam / interview, just use the built in sorting function of a language.
@sunnyyasser56257 ай бұрын
How do you even perform binary search on a linked list? Random access iterators are not available
@julioflores18497 ай бұрын
I’m pretty sure it is to be sorted and the linked list needs to have a total node count
@Schnorzel13377 ай бұрын
You basicly run past nodes not looking at their value. So you can not effectively do binary search on a linked list.
@matthewgraham7907 ай бұрын
you still need to look at their 'next' pointer so by omitting looking at the value and comparing, you are still only diving the time by roughly 3, which although good, is still O(n) not O(ln n)@@Schnorzel1337
@TehIdiotOne7 ай бұрын
It's not a linked list, where'd you get that idea? FYI dynamic arrays are called ArrayLists in Java.
@lundril2 ай бұрын
Because it's called "List" and not "Vector" or "Array" ???
@notro657Ай бұрын
I hate the fact that even if he wrote a very bad code they laugh at him, not cool.
@ehfoss23 сағат бұрын
The scene is a bit deeper. Richard is a low level coder who is used to squeezing out loads of perf. In C and C++ it's known that a linear walk of an array will be faster in many cases where the array is rather small (say,
@steves925014 күн бұрын
I’ve seen lists before that were called “sortedList” that aren’t in fact sorted😮
@tungpho21327 ай бұрын
Wow, actually rarely to see someone beat 100% of runtime 😮
@Mankind54907 ай бұрын
Bro really knew the LeetCode Number lmfao
@JaredT7 ай бұрын
This channel has potential to hit a million subs 🚀
@NeetCodeIO7 ай бұрын
Now that i think of it, i should really start uploading on the main channel again. I need to hit one milli before the end of the year 😤😤😤
@upsidedownChad7 ай бұрын
@@NeetCodeIO I'll watch every video you post on my phone, computer and tv at the same time
@liquidsnake68793 күн бұрын
Interview questions consisting of bs nobody actually does in the real world
@CallousCoder7 ай бұрын
Memory used 45MB?!?! I used to run a whole fucking file server with 32MB with a 100 clients attached. Also this isn't that trivia depending on the size of the list. As getting the length and dividing it and then doing the branch logic may well be slower on a list with a certain amount of entries (depending what also fits into the cpu cache).
@Terrados13377 ай бұрын
Back when I learned about search algorithms in school I was amazed. So much performance! Now that I have to handle actual data I rarely get to us any. Since data is complete diarrhea 99% of the time might as well map it. Too much memory usage? Download more RAM, duh!
@dineshvikramvenkatesh4911Ай бұрын
Isn't the code actually wrong? Wouldn't it throw an index out of bounds exception when sorted list is empty?
@sprytnychomik7 ай бұрын
Not sure that binary search would be so much better on a sorted (assuming linked) *list*, though.
@ValerieLoveTV7 ай бұрын
Linked lists aren't accessible by index.
@TragicGFuel7 ай бұрын
What kinda indexed linked lists are you playing around with?
@sprytnychomik7 ай бұрын
@@ValerieLoveTVThis one apparently is.
@isodoubIet7 ай бұрын
@@ValerieLoveTV They can be if you write the api to do it. In Haskell for example they are.
@ramanchaudhary251819 күн бұрын
@@TragicGFuel these are most likely Java array lists
@EudaderurScheiss7 ай бұрын
i actually like the first solution more. it is standard pattern and you see what it does in a second. the l+r stuff is nice to have for high performance areas. but i dont understand leetcode. if i want to program or have it hard, i start game dev and waste time there
@joshmosley19 күн бұрын
The first solution is extremely slow, and the 'hard' one is extremely fast. It doesn't really matter for small things because computers are very fast anyway - but when you're dealing with a large array it makes a big difference. Leetcode and Data Structures/Algorithms in general is about learning the "right" way to do things as a programmer and even beyond that, learning WHY the 2nd answer is correct. The concepts are very useful if you can get them down. Common sense guide to DSA was a very helpful book for me to begin learning DSA concepts.
@monkemode812817 күн бұрын
@@joshmosley Yeah, this depends on the usage. If you know your code will only deal with a few elements, it's fine code. Say it's an API call and there's a cap of 100 elements. However, you should be able to understand binary search. Besides, it's pretty intuitive and simple, especially if you look at non-code practical examples. For example the way people look for a page in a long book is akin to binary search but it's not binary it's more... random.
@CottidaeSEA10 күн бұрын
@@joshmosley The first one is only slow under the assumption that the list is sorted. It was in this case, but you're not always working with a sorted list, or at least not sorted in the way you want to search.
@lesterdelacruz5088Ай бұрын
The code in the show will never have a -1 output. It will find the last index in the case where the item isn’t there.
@Cammymoop7 ай бұрын
I've never written a binary search algorithm in my life, I always know what the index is anyway
@lukaszwinnicki688016 күн бұрын
He should own it and say that it was a joke to see if it will pass on the review.
@yashgangrade54607 ай бұрын
Even though I've seen this before, watching it again with Neetcode makes it even funnier.
@sonicfind7 ай бұрын
They get the value at an index... *before* checking if the index is valid? Imma just use that with an empty list. That should be fun.
@monkemode812817 күн бұрын
Honestly not bad, IDC. Depends on the use.
@joonotfins4 күн бұрын
What season was this? I don’t remember seeing this
@pencilcheck7 ай бұрын
Just nit picking the naming of this title. Brute Force usually implied trying all possible solutions in a solution space. Those terms usually applied in optimization problems. Search in a 1d array isn't an optimization problem.
@RadenVijayaАй бұрын
If the element count is small, e.g.
@Dan-codes4 күн бұрын
If interviewers laugh at a person for their solution, instead of helping the candidate learn, they are unfit for their role. They could be your boss someday or a connection at a company you want to work for. Candidates are nervous and show up for free. Meanwhile, you're getting paid to watch them. They deserve professionalism, at the very least. I do lots of interviews(real and mock) and *always* root for the interviewee.
@ChrisBakare7 ай бұрын
High school never ends
@DevloupАй бұрын
im getting confused with you calling pointers to variables (or index used?) mmm am i missing something? i ask cause none noticed it so i think im wrong
@kebien602010 күн бұрын
I think it's just because they're so similar. A pointer is just an index into the virtual memory space of a process, and an index is just a pointer into a sub-region of memory
@sergrojGrayFace7 ай бұрын
Also, never write binary search for a simple list, because you'll probably find a standard implementation for it in any language.
@James-l5s7k7 ай бұрын
"int index = 0;" -> how the hell is this a pointer?
@phoneix24886Ай бұрын
You will ve surprised how many java 6/7 code that exists till date has several such code. The devs might have outgrown themselves but the code remains. Its not embarrassing. Its lazy work.
@marcotroster82472 ай бұрын
The criticism isn't justified at all unless you come up with a proper benchmark. He should have used the standard indexOf() function though. It reads a lot better.
@kamurashev3 күн бұрын
In java you can access array element by square brackets but not the array list element. Other languages also have accessors for collection elements, even python. Nothing wrong with it. Moreover the main issue is the code is garbage.
@chemaalonso79317 ай бұрын
Who need brackets? Is an ArrayList and is SO MUCH DIFFERENT from an Array, in JAVA you DONT need a fucking method to get an element.
@ThomasGiles12 күн бұрын
What was the name of that show again?
@groot32717 ай бұрын
Which video is that. Can someone share the link??
@SashaCianuro7 ай бұрын
TV series Silicon valley
@tmerb7 ай бұрын
"to turn up the difficulty lets do it in java" 🤣
@nehadyounis7 ай бұрын
bro said "this is leetcode 704" and it was actually leetcode 704
@subhambiswas8374Ай бұрын
Which show is it ?
@Sharmav037 ай бұрын
Bro pulled the question number out of his ass and got it right lmao!
@akialter7 ай бұрын
You’ll be surprised how many average dev cant write a bug free binary search
@mycommentmyopinion7 ай бұрын
Tbf I haven't had a case where I had to implement binary search myself and I think that goes for most devs
@anon_y_mousse7 ай бұрын
@@mycommentmyopinion As an exercise, write your own libc. Go back and do it every 5 to 10 years.
@JosephGleespen7 ай бұрын
@@mycommentmyopinionlmfao it's not about how much it comes up at work, it's a test, are you all being purposely dense about this? It is an EASY way for companies to TEST if they want to hire you. Jfc.
@mycommentmyopinion7 ай бұрын
@@anon_y_mousse Good idea, gonna try that
@blasttrash7 ай бұрын
Well even oracle jdk apparently had a bug in binary search for many years. And JVM is said to be one of the most optimized software written. So even the geniuses at Oracle made binary search mistakes :P
@fuzzy-022 күн бұрын
Bro literally quotes what book in the library, row by column, then which page and which line. X.X
@davidr.6037 ай бұрын
So you just split the list in the half, and then search the right half. Why dont you split it multiple times, for a small list like in the example it might not be viable but for an even bigger list it for sure would be. Or am i thinking wrong here
@arjundureja7 ай бұрын
It's a loop. The list is constantly being split until the target is found or the left pointer is greater than the right pointer
@davidr.6037 ай бұрын
@@arjundureja gotcha, thank you for explaining
@James-l5s7k7 ай бұрын
it's absolutely readable code; if you know C XD
@parryhotter93337 ай бұрын
why do you call an index a pointer? they are two very different things
@groff86577 ай бұрын
He meant value pointers in DSA not memory pointers in C++.
@parryhotter93337 ай бұрын
@@groff8657 Since the programming language in question is Java, he most certainly should have said "index" because the .get method on a list takes an integer to index into the list and not a pointer. A pointer is not even a defined datatype in Java so using this keyword in this context is not even appropriate. If you really want to be fringe, then you might argue that a variable reference to an object is a "pointer", however this is incorrect as Java never actually provides direct references to objects with an address (which is the definition of a pointer to an object), but rather to a proxy that keeps track of the object. An apt name for a strategy that encapsulates this behavior is therefore "pass-by-proxy". In any case, a "value pointer" is not even a recognized term in DSA, so why even bring this up? edit: this is not to discredit NeetCode in any way, but as a channel that teaches and talks about programming, he should be precise in his wording and explanation. With that said, it is a nice video and not all that serious.
@LeeO0704Ай бұрын
why is he calling variable "pointers"?
@bigbigdog7 ай бұрын
If I am not mistaken that while loop does nothing. Notice the semi-colon at the end of line 114. That closed the while loop.
@anon_y_mousse7 ай бұрын
Notice the ++index in the condition header? Yeah, that's the loop.
@asagiai49657 ай бұрын
Technically it does something. It is increasing the index. The index which the item is on will exit the loop thus giving the answer
@Kurushimi17297 ай бұрын
It's a funny way to write a loop (at least I haven't seen it before) but it looks valid. I guess they were trying to be terse.
@bigbigdog7 ай бұрын
@@asagiai4965 "Technically"
@bigbigdog7 ай бұрын
@@anon_y_mousse no that's what cause the loop to end. The loop itself still does nothing... other than increasing the index.
@Simon-xi8tb17 күн бұрын
Noone gonna mention that mr. Neetcode looks like the guy in red shirt
@B_dev14 күн бұрын
If this was me I'd retire on the spot SMH my head
@janisir4529Ай бұрын
It's fine for small arrays, and when you are in a hurry. ¯\_(ツ)_/¯
@ballakobe247 ай бұрын
I also watched Silicon Valley for the code snippets.
@brycejohansen71144 ай бұрын
Well at least he didn't do a bogosort.
@avshur22 сағат бұрын
Is that production code? Writing a search algorythm by hand in Java in production code- that should be funny.
@asagiai49657 ай бұрын
To be fair he might not have known it or forgot about it.
@FM1234Ай бұрын
Bro calls and index a pointer
@hendrikmartina35527 ай бұрын
The guy have my same name. But I will never write that monstrosity
@QuanNguyen-og6pq7 ай бұрын
It's O(n), it's not the slowest subroutine in history (That would be my response xD)
@Veso26619 сағат бұрын
What kind of crap is this Humiliating people who just want a job to feed their family Maybe you are smart now, but dont worry there is and will always be someone smarter
@abdulmuqsit75687 ай бұрын
Who do I get to be as good as you at leetcode bro?
@daviddickey983214 күн бұрын
Why arent you using a hash map!? REEEEEE
@div_tm7 ай бұрын
This only matters if the code segment is performance critical, otherwise probably unnecessary to do a binary search. It's better to optimize the critical parts and leave the other parts as simple as possible. Anyway, nothing to laugh at, really.
@edthelazyboy7 ай бұрын
I would also agree. The firmware code base I worked on for has various non performance command handlers for calling functions based on opcode used linear search. They are straightforward to read and historically functioned reliably. Changes are often made to the opcode / function pointer table so the table isn't guaranteed to be sorted anyways if someone accidentally adds a table entry out of order based on the opcode. Sometimes, better readability and maintainability is more important.
@taragnor17 күн бұрын
Yup this is the kind of thinking that separate practical programmers who get stuff done versus the perfectionist armchair guys that spend their time doing leetcode and nitpicking.
@CodingMachine0077 ай бұрын
Like if you tried to increase volume through KZbin's volume icon on video 🤣
@jaywin53737 ай бұрын
In the real-world interview, you make a stupid mistake, and they will make you dumber than usual.