Dev HUMILIATED after writing Brute Force Search on a Sorted List

  Рет қаралды 176,132

NeetCodeIO

NeetCodeIO

Күн бұрын

🚀 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
@NeetCodeIO
@NeetCodeIO 7 ай бұрын
Kinda surprised how many people think it's a Linked List.. does this look familiar: List list = new ArrayList();
@rinat2160
@rinat2160 7 ай бұрын
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
@IronicHavoc Ай бұрын
​@rinat2160 its a leetcode question though, not a library
@IronicHavoc
@IronicHavoc Ай бұрын
​@rinat2160 Also what do you mean by "random" iterator? Do you mean, like, arbitrary/generic?
@KeepOnBlazing
@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
@LeeO0704
@LeeO0704 29 күн бұрын
why is he calling variable "pointers"?
@taaaaaaay
@taaaaaaay 7 ай бұрын
Bro was able to identify the Leetcode number from the top of his head💀
@chillsjiujitsu
@chillsjiujitsu 7 ай бұрын
That’s why he’s the GOAT
@aflous
@aflous 7 ай бұрын
His name is Neetcode, what did you expect?
@VampiricBard
@VampiricBard 7 ай бұрын
@@aflous 🤣
@xcuu
@xcuu 7 ай бұрын
Tell me you’re a sex haver without telling me you’re a sex haver
@kingpen5866
@kingpen5866 7 ай бұрын
Bro
@LandoKalrissian
@LandoKalrissian 7 ай бұрын
For small arrays, linear search actually beats binary search because it's so branch-prediction friendly.
@NeetCodeIO
@NeetCodeIO 7 ай бұрын
Also why didnt he just do .indexOf(element)
@LandoKalrissian
@LandoKalrissian 7 ай бұрын
​@@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.
@CrabGuyy
@CrabGuyy 7 ай бұрын
@@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
@ecchioni
@ecchioni 7 ай бұрын
Same reason why C# uses quicksort and insertion sort combo as its default sort.
@TacoMaster07
@TacoMaster07 7 ай бұрын
@@CrabGuyy cache locality and branch predictability effects on different DS and algorithm ordinance is obvious? Dumbass.
@halfjainam
@halfjainam 7 ай бұрын
did he just fucking quote a LEETCODE question with NUMBER by looking 4 lines of code!!! 😭😭
@someoneontheweb4515
@someoneontheweb4515 7 ай бұрын
its not that big of a deal lmao
@rockdude1122
@rockdude1122 7 ай бұрын
@@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-ev6xq
@Random-ev6xq 2 ай бұрын
bro im sayin i thought i was tweakin 😂😂😂
@monkemode8128
@monkemode8128 17 күн бұрын
@@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).
@GnomeEU
@GnomeEU 7 ай бұрын
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!
@JosephGleespen
@JosephGleespen 7 ай бұрын
It's a leetcode problem bro, that's what they are like
@mycommentmyopinion
@mycommentmyopinion 7 ай бұрын
@@JosephGleespen Literally not the point.
@NeetCodeIO
@NeetCodeIO 7 ай бұрын
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-og6pq
@QuanNguyen-og6pq 7 ай бұрын
yup. Bad sql, bad joins has caused more slowness than any search problems I've seen.
@talareese580
@talareese580 7 ай бұрын
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.
@shobhandash8720
@shobhandash8720 7 ай бұрын
the bully: good, now do this in O(1)
@sharpfang
@sharpfang 7 ай бұрын
ret=-1; for (i=0; i
@Zattilio
@Zattilio Ай бұрын
Ok i will use a hashmap
@thepriestofvaranasi
@thepriestofvaranasi 25 күн бұрын
Proceeds to create a hashmap 🗿🗿
@LukeDrumm
@LukeDrumm 7 ай бұрын
Discussion of performance without mentioning CPU caching is ... interesting.
@elijahbuscho7715
@elijahbuscho7715 7 ай бұрын
This is why I use PHP and throw performance out the window
@niggacockball7995
@niggacockball7995 7 ай бұрын
@@elijahbuscho7715 unironically based, only old hags with shitpads from 2001 complain about performance like bruv just get a new pc its not that hard
@TehIdiotOne
@TehIdiotOne 7 ай бұрын
@@elijahbuscho7715 I do Python
@naughtiousmaximus7853
@naughtiousmaximus7853 Ай бұрын
​@@elijahbuscho7715 No need to solve a problem if there isnt one,
@JNelson_
@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.
@444limm
@444limm 7 ай бұрын
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
@nikhilchouhan1802
@nikhilchouhan1802 2 ай бұрын
or just use std::optional
@nikhilchouhan1802
@nikhilchouhan1802 2 ай бұрын
in c++ ofc
@justADeni
@justADeni 7 ай бұрын
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.
@LeeO0704
@LeeO0704 29 күн бұрын
why is he calling variable "pointers"?
@monkemode8128
@monkemode8128 17 күн бұрын
@@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.
@EvanPilb
@EvanPilb 8 күн бұрын
2:00 bro it's your twin
@n_x1891
@n_x1891 7 ай бұрын
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
@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.
@monkemode8128
@monkemode8128 17 күн бұрын
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.
@jibreelkeddo7030
@jibreelkeddo7030 7 күн бұрын
@@monkemode8128 The downside of doing that is that you end up covering up what should be a bug report to the backend guy.
@NeetCodeIO
@NeetCodeIO 7 ай бұрын
I think the more embarrassing thing is the person using a Windows laptop at 1:18
@NeetCodeIO
@NeetCodeIO 7 ай бұрын
I'm tempted to delete your comment and edit mine...
@abdalla4425
@abdalla4425 7 ай бұрын
LMAOO @@NeetCodeIO
@pengie_
@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
@jonathanng7141
@jonathanng7141 7 ай бұрын
What is wrong with Microsoft surface laptop?
@GreatTaiwan
@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)
@kebien6020
@kebien6020 10 күн бұрын
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_
@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.
@nemikuuro
@nemikuuro 7 ай бұрын
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?
@romanivanov6183
@romanivanov6183 7 ай бұрын
Hey! l +r ?? You will get integer overflow!
@NeetCodeIO
@NeetCodeIO 7 ай бұрын
🤦i've dishonored my entire family
@oussamabennasr4936
@oussamabennasr4936 7 ай бұрын
m = l + (r-l)/2
@blasttrash
@blasttrash 7 ай бұрын
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.
@se4geniuses
@se4geniuses 7 ай бұрын
@romanivanov True, but if you're iterating over more than 2^((sizeof(int) * 8) - 1) / 2 elements, you have bigger concerns to deal with.
@TehIdiotOne
@TehIdiotOne 7 ай бұрын
@@se4geniuses You do, but it's not entirely unheard of
@fromjavatohaskell909
@fromjavatohaskell909 7 ай бұрын
3:44 (l + r)/2 would cause overflow for big arrays
@tylerd5924
@tylerd5924 Ай бұрын
🤓☝🏻
@jamjam3448
@jamjam3448 Ай бұрын
Yeah l+(r-l) /2 is better
@ursalight
@ursalight 21 күн бұрын
@@calculovo4219 oh right
@yashmalviya0149
@yashmalviya0149 20 күн бұрын
(l + ((r - l) >> 1)) 😎
@monkemode8128
@monkemode8128 17 күн бұрын
What about....... l/2 + r/2 Seems intuitive to me.
@SuperMaDBrothers
@SuperMaDBrothers 7 ай бұрын
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
@234lk
@234lk 7 ай бұрын
Do you really think anyone would watch the show if they acted like software engineers?
@SuperMaDBrothers
@SuperMaDBrothers 7 ай бұрын
@@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!
@mycommentmyopinion
@mycommentmyopinion 7 ай бұрын
I doubt it's made to be realistic lol, nobody would want to watch a realistic version
@anon_y_mousse
@anon_y_mousse 7 ай бұрын
@@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
@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
@unixpro1970
@unixpro1970 7 ай бұрын
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 🤡
@asandax6
@asandax6 7 ай бұрын
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
@amzker Ай бұрын
Yes, exactly
@ramanchaudhary2518
@ramanchaudhary2518 19 күн бұрын
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
@taragnor
@taragnor 17 күн бұрын
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.
@mukeshrawat1304
@mukeshrawat1304 7 ай бұрын
Guess what?! I landed on Neetcode's YT channel while doing a brute force linear search on KZbin. Definitely worth it ❤😊
@ofbaran
@ofbaran 7 ай бұрын
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.
@ofbaran
@ofbaran 7 ай бұрын
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
@ofbaran
@ofbaran 7 ай бұрын
The linear search is still useful if the array is unsorted, I'm sorry.
@ishanbajpai6940
@ishanbajpai6940 7 ай бұрын
Silicon Valley, baby!!!
@vinayemani4105
@vinayemani4105 7 ай бұрын
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
@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.
@thimowellner7686
@thimowellner7686 8 күн бұрын
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.
@cwagnello
@cwagnello 22 сағат бұрын
@@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.
@CottidaeSEA
@CottidaeSEA 10 күн бұрын
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.
@mycommentmyopinion
@mycommentmyopinion 7 ай бұрын
Fuccc I think I need a refresh on algorithms, I use them so rarely I keep forgetting how they work lol
@chandrap8391
@chandrap8391 7 ай бұрын
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.
@connormc711
@connormc711 7 ай бұрын
Wow. I have never seen someone with that close of an approximation to real skills
@TacoMaster07
@TacoMaster07 7 ай бұрын
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.
@jonathanng7141
@jonathanng7141 7 ай бұрын
I just learnt sorting this week and this video was perfect in helping me understand how the code is implemented!
@kantamei7707
@kantamei7707 4 күн бұрын
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_
@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
@mgancarzjr Ай бұрын
In case anyone needs to see this: int mid = left + (right - left) / 2;
@monkemode8128
@monkemode8128 17 күн бұрын
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
@steffenrumpel2784
@steffenrumpel2784 7 күн бұрын
@@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.
@monkemode8128
@monkemode8128 7 күн бұрын
​@@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.
@dracsharp
@dracsharp 7 ай бұрын
From a business perspective and assuming you are building an MVP or a possible feature, the fastest to add solution is the right solution.
@xavier7769
@xavier7769 7 ай бұрын
well bin search is fast to add he did it in like 2 mins
@mycommentmyopinion
@mycommentmyopinion 7 ай бұрын
@@xavier7769 With a bug that could cost money and time to debug :D
@def__init
@def__init 7 ай бұрын
Skill issue
@rinat2160
@rinat2160 7 ай бұрын
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).
@AzidHouse
@AzidHouse 7 ай бұрын
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.
@ramanchaudhary2518
@ramanchaudhary2518 19 күн бұрын
The .size() method returns an integer value if that’s what you meant ?
@AzidHouse
@AzidHouse 19 күн бұрын
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.
@imerence6290
@imerence6290 2 ай бұрын
I'm actually impressed how short the code is.
@shardator
@shardator 5 күн бұрын
Linear search can be a lot faster than binary search if the elements are stored consecutively in memory, and the list is relatively small.
@lmao4982
@lmao4982 7 ай бұрын
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_gold
@69k_gold 7 ай бұрын
Wouldn't this be a massive code duplication issue? He's reinventing the wheel
@Dom-zy1qy
@Dom-zy1qy 7 ай бұрын
This dude writes binary search like I breathe.
@edwardmitchell6581
@edwardmitchell6581 7 ай бұрын
It's basically his full time job.
@ceciljoel9577
@ceciljoel9577 7 ай бұрын
For seniors those algorithms for really that easy
@yt-sh
@yt-sh 2 ай бұрын
binary search is a pretty good algorithm, only problem is arrays have to be sorted first...
@hassaniq0777
@hassaniq0777 2 ай бұрын
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-xq2xm
@DankMemes-xq2xm 16 күн бұрын
@@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.
@sunnyyasser5625
@sunnyyasser5625 7 ай бұрын
How do you even perform binary search on a linked list? Random access iterators are not available
@julioflores1849
@julioflores1849 7 ай бұрын
I’m pretty sure it is to be sorted and the linked list needs to have a total node count
@Schnorzel1337
@Schnorzel1337 7 ай бұрын
You basicly run past nodes not looking at their value. So you can not effectively do binary search on a linked list.
@matthewgraham790
@matthewgraham790 7 ай бұрын
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
@TehIdiotOne
@TehIdiotOne 7 ай бұрын
It's not a linked list, where'd you get that idea? FYI dynamic arrays are called ArrayLists in Java.
@lundril
@lundril 2 ай бұрын
Because it's called "List" and not "Vector" or "Array" ???
@notro657
@notro657 Ай бұрын
I hate the fact that even if he wrote a very bad code they laugh at him, not cool.
@ehfoss
@ehfoss 23 сағат бұрын
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,
@steves9250
@steves9250 14 күн бұрын
I’ve seen lists before that were called “sortedList” that aren’t in fact sorted😮
@tungpho2132
@tungpho2132 7 ай бұрын
Wow, actually rarely to see someone beat 100% of runtime 😮
@Mankind5490
@Mankind5490 7 ай бұрын
Bro really knew the LeetCode Number lmfao
@JaredT
@JaredT 7 ай бұрын
This channel has potential to hit a million subs 🚀
@NeetCodeIO
@NeetCodeIO 7 ай бұрын
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 😤😤😤
@upsidedownChad
@upsidedownChad 7 ай бұрын
​@@NeetCodeIO I'll watch every video you post on my phone, computer and tv at the same time
@liquidsnake6879
@liquidsnake6879 3 күн бұрын
Interview questions consisting of bs nobody actually does in the real world
@CallousCoder
@CallousCoder 7 ай бұрын
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).
@Terrados1337
@Terrados1337 7 ай бұрын
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
@dineshvikramvenkatesh4911 Ай бұрын
Isn't the code actually wrong? Wouldn't it throw an index out of bounds exception when sorted list is empty?
@sprytnychomik
@sprytnychomik 7 ай бұрын
Not sure that binary search would be so much better on a sorted (assuming linked) *list*, though.
@ValerieLoveTV
@ValerieLoveTV 7 ай бұрын
Linked lists aren't accessible by index.
@TragicGFuel
@TragicGFuel 7 ай бұрын
What kinda indexed linked lists are you playing around with?
@sprytnychomik
@sprytnychomik 7 ай бұрын
@@ValerieLoveTVThis one apparently is.
@isodoubIet
@isodoubIet 7 ай бұрын
@@ValerieLoveTV They can be if you write the api to do it. In Haskell for example they are.
@ramanchaudhary2518
@ramanchaudhary2518 19 күн бұрын
⁠​⁠​⁠​⁠​⁠​⁠​⁠​⁠​⁠​⁠@@TragicGFuel these are most likely Java array lists
@EudaderurScheiss
@EudaderurScheiss 7 ай бұрын
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
@joshmosley
@joshmosley 19 күн бұрын
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.
@monkemode8128
@monkemode8128 17 күн бұрын
@@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.
@CottidaeSEA
@CottidaeSEA 10 күн бұрын
@@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
@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.
@Cammymoop
@Cammymoop 7 ай бұрын
I've never written a binary search algorithm in my life, I always know what the index is anyway
@lukaszwinnicki6880
@lukaszwinnicki6880 16 күн бұрын
He should own it and say that it was a joke to see if it will pass on the review.
@yashgangrade5460
@yashgangrade5460 7 ай бұрын
Even though I've seen this before, watching it again with Neetcode makes it even funnier.
@sonicfind
@sonicfind 7 ай бұрын
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.
@monkemode8128
@monkemode8128 17 күн бұрын
Honestly not bad, IDC. Depends on the use.
@joonotfins
@joonotfins 4 күн бұрын
What season was this? I don’t remember seeing this
@pencilcheck
@pencilcheck 7 ай бұрын
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
@RadenVijaya Ай бұрын
If the element count is small, e.g.
@Dan-codes
@Dan-codes 4 күн бұрын
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.
@ChrisBakare
@ChrisBakare 7 ай бұрын
High school never ends
@Devloup
@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
@kebien6020
@kebien6020 10 күн бұрын
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
@sergrojGrayFace
@sergrojGrayFace 7 ай бұрын
Also, never write binary search for a simple list, because you'll probably find a standard implementation for it in any language.
@James-l5s7k
@James-l5s7k 7 ай бұрын
"int index = 0;" -> how the hell is this a pointer?
@phoneix24886
@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.
@marcotroster8247
@marcotroster8247 2 ай бұрын
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.
@kamurashev
@kamurashev 3 күн бұрын
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.
@chemaalonso7931
@chemaalonso7931 7 ай бұрын
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.
@ThomasGiles
@ThomasGiles 12 күн бұрын
What was the name of that show again?
@groot3271
@groot3271 7 ай бұрын
Which video is that. Can someone share the link??
@SashaCianuro
@SashaCianuro 7 ай бұрын
TV series Silicon valley
@tmerb
@tmerb 7 ай бұрын
"to turn up the difficulty lets do it in java" 🤣
@nehadyounis
@nehadyounis 7 ай бұрын
bro said "this is leetcode 704" and it was actually leetcode 704
@subhambiswas8374
@subhambiswas8374 Ай бұрын
Which show is it ?
@Sharmav03
@Sharmav03 7 ай бұрын
Bro pulled the question number out of his ass and got it right lmao!
@akialter
@akialter 7 ай бұрын
You’ll be surprised how many average dev cant write a bug free binary search
@mycommentmyopinion
@mycommentmyopinion 7 ай бұрын
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_mousse
@anon_y_mousse 7 ай бұрын
@@mycommentmyopinion As an exercise, write your own libc. Go back and do it every 5 to 10 years.
@JosephGleespen
@JosephGleespen 7 ай бұрын
​@@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.
@mycommentmyopinion
@mycommentmyopinion 7 ай бұрын
@@anon_y_mousse Good idea, gonna try that
@blasttrash
@blasttrash 7 ай бұрын
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-02
@fuzzy-02 2 күн бұрын
Bro literally quotes what book in the library, row by column, then which page and which line. X.X
@davidr.603
@davidr.603 7 ай бұрын
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
@arjundureja
@arjundureja 7 ай бұрын
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.603
@davidr.603 7 ай бұрын
@@arjundureja gotcha, thank you for explaining
@James-l5s7k
@James-l5s7k 7 ай бұрын
it's absolutely readable code; if you know C XD
@parryhotter9333
@parryhotter9333 7 ай бұрын
why do you call an index a pointer? they are two very different things
@groff8657
@groff8657 7 ай бұрын
He meant value pointers in DSA not memory pointers in C++.
@parryhotter9333
@parryhotter9333 7 ай бұрын
@@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
@LeeO0704 Ай бұрын
why is he calling variable "pointers"?
@bigbigdog
@bigbigdog 7 ай бұрын
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_mousse
@anon_y_mousse 7 ай бұрын
Notice the ++index in the condition header? Yeah, that's the loop.
@asagiai4965
@asagiai4965 7 ай бұрын
Technically it does something. It is increasing the index. The index which the item is on will exit the loop thus giving the answer
@Kurushimi1729
@Kurushimi1729 7 ай бұрын
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.
@bigbigdog
@bigbigdog 7 ай бұрын
@@asagiai4965 "Technically"
@bigbigdog
@bigbigdog 7 ай бұрын
​@@anon_y_mousse no that's what cause the loop to end. The loop itself still does nothing... other than increasing the index.
@Simon-xi8tb
@Simon-xi8tb 17 күн бұрын
Noone gonna mention that mr. Neetcode looks like the guy in red shirt
@B_dev
@B_dev 14 күн бұрын
If this was me I'd retire on the spot SMH my head
@janisir4529
@janisir4529 Ай бұрын
It's fine for small arrays, and when you are in a hurry. ¯\_(ツ)_/¯
@ballakobe24
@ballakobe24 7 ай бұрын
I also watched Silicon Valley for the code snippets.
@brycejohansen7114
@brycejohansen7114 4 ай бұрын
Well at least he didn't do a bogosort.
@avshur
@avshur 22 сағат бұрын
Is that production code? Writing a search algorythm by hand in Java in production code- that should be funny.
@asagiai4965
@asagiai4965 7 ай бұрын
To be fair he might not have known it or forgot about it.
@FM1234
@FM1234 Ай бұрын
Bro calls and index a pointer
@hendrikmartina3552
@hendrikmartina3552 7 ай бұрын
The guy have my same name. But I will never write that monstrosity
@QuanNguyen-og6pq
@QuanNguyen-og6pq 7 ай бұрын
It's O(n), it's not the slowest subroutine in history (That would be my response xD)
@Veso266
@Veso266 19 сағат бұрын
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
@abdulmuqsit7568
@abdulmuqsit7568 7 ай бұрын
Who do I get to be as good as you at leetcode bro?
@daviddickey9832
@daviddickey9832 14 күн бұрын
Why arent you using a hash map!? REEEEEE
@div_tm
@div_tm 7 ай бұрын
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.
@edthelazyboy
@edthelazyboy 7 ай бұрын
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.
@taragnor
@taragnor 17 күн бұрын
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.
@CodingMachine007
@CodingMachine007 7 ай бұрын
Like if you tried to increase volume through KZbin's volume icon on video 🤣
@jaywin5373
@jaywin5373 7 ай бұрын
In the real-world interview, you make a stupid mistake, and they will make you dumber than usual.
Creators of Devin AI are genius competitive programmers?
20:23
NeetCodeIO
Рет қаралды 190 М.
The 3 Laws of Writing Readable Code
5:28
Kantan Coding
Рет қаралды 611 М.
РОДИТЕЛИ НА ШКОЛЬНОМ ПРАЗДНИКЕ
01:00
SIDELNIKOVVV
Рет қаралды 3,6 МЛН
This mother's baby is too unreliable.
00:13
FUNNY XIAOTING 666
Рет қаралды 31 МЛН
Players vs Corner Flags 🤯
00:28
LE FOOT EN VIDÉO
Рет қаралды 101 МЛН
The Problem with Object-Oriented Programming
8:21
NeetCodeIO
Рет қаралды 156 М.
Math News: The Bunkbed conjecture was just disproven!!!!!!!
14:59
Dr. Trefor Bazett
Рет қаралды 61 М.
Why Can't CPU's Do Math?
2:30
Yashmerino
Рет қаралды 28 М.
before you code, learn how computers work
7:05
Low Level
Рет қаралды 409 М.
Migration Lesson: Don't Use Prisma | Prime Reacts
29:16
ThePrimeTime
Рет қаралды 155 М.
Can you steal something that's already free?
11:48
NeetCodeIO
Рет қаралды 38 М.
I Hacked a Discord Bot, the Owner said this...
9:09
No Text To Speech
Рет қаралды 1,3 МЛН
Programmers in 2024 have no Deep Knowledge
8:38
NeetCodeIO
Рет қаралды 192 М.
I Quit Amazon after 2 Months | Reaction
29:29
NeetCodeIO
Рет қаралды 137 М.
Microservices are Technical Debt
31:59
NeetCodeIO
Рет қаралды 472 М.