Top 8 Data Structures for Coding Interviews

Поделиться
HTML-код
  • Опубликовано: 6 ноя 2024

Комментарии • 159

  • @NeetCode
    @NeetCode  2 года назад +26

    🚀 neetcode.io/ - I created a FREE site to make interview prep a lot easier, hope it helps! ❤

  • @anthonyuccello1458
    @anthonyuccello1458 2 года назад +306

    Hes at Google but he doesn’t stop. A true champion!

    • @Labandusette
      @Labandusette 2 года назад +22

      The hero we don't deserve.

    • @shreehari2589
      @shreehari2589 2 года назад +7

      Coz that's what heroes do

    • @iankitveer
      @iankitveer Год назад +3

      He’s at Google yet teaches linked list concept wrong

  • @daringlybad
    @daringlybad 2 года назад +270

    Just landed a job at google thanks to you Neetcode! Weeks of falling asleep to your videos and studying my ass off really paid off and I really appreciate your work!

    • @NeetCode
      @NeetCode  2 года назад +34

      Nice, congrats!!!

    • @darkshadow_323
      @darkshadow_323 2 года назад +3

      @@NeetCode Could you please do a video on matrix chain multiplication in dynamic programming? your explanations are clear and concise and better than other explanations

    • @035asadali8
      @035asadali8 2 года назад

      @@darkshadow_323 u done strassens matrix multiplication? its n^2.7 tc

    • @programming3043
      @programming3043 2 года назад

      What was your preferred choice of language for interview?

    • @daringlybad
      @daringlybad 2 года назад

      @@NeetCode

  • @superbmood
    @superbmood 2 года назад +67

    A must watch for anyone grinding for tech interviews

  • @k.i.m.5506
    @k.i.m.5506 2 года назад +37

    3:50
    incorrect. insert or delete in middle of the linked list is not O(1).
    you need to find the node before insert or delete it, which is O(n) for the linked list

    • @mixtli1975
      @mixtli1975 2 месяца назад +2

      It's not "incorrect". Insertion itself is constant time, and there are cases where you already have the node you want to insert after. With an array, even if you know the index you want to insert at, it's O(n), so it's different. It is true that if you don't have a reference to the node you want to insert after, you'll have to search for it first, which is O(n).

  • @nightmarauder2909
    @nightmarauder2909 2 года назад +9

    Hey man, I just signed an offer from Google today and I feel like I couldn't do this without you! Many many thanks for your videos!

  • @guptaanmol184
    @guptaanmol184 2 года назад +23

    Correction:
    Removing last node in a linked list:
    The time complexity of removing the last node in a linked list is O(n) and NOT O(1). To remove the last node of the linked list, we would have to traverse the linked list to the second last node (ie. node.next.next == null) and then remove the last node (ie. set node.next = null).

    • @ianakotey
      @ianakotey Год назад +13

      @@asaprogrammer_ correction on you. To remove the tail, you need to know the node before the tail (to point it to null). Unless you explicitly store it, you need to traverse the list to find it O(n)

    • @asaprogrammer_
      @asaprogrammer_ Год назад +3

      @@ianakotey yep, you're right. I forgot to mention that you need to store it to get O(1) Thanks!

    • @Y2B123
      @Y2B123 Год назад +8

      Removing elements from linked lists by “index” should also take O(n).

  • @joddeveloper7643
    @joddeveloper7643 Год назад +14

    I think u need to iterate over the linked list to know the middle(as in the size) then iterate till you find the value in the linked list

    • @ezrahuffman
      @ezrahuffman Год назад +1

      Most implementations of a linked list track the size as you add/remove things so it is constant time to check the size. Depends on implementation though.

    • @Ford-ju9yr
      @Ford-ju9yr 6 месяцев назад

      ​@@ezrahuffman still O(n) to get to the middle even when you know lists length. Unless you maintain relevant pointer to the middle somehow

  • @hashi856
    @hashi856 8 месяцев назад +1

    Insertion into the middle of a linked list is not constant. It’s O(N). You have to traverse the list to get to the node you want to insert. Same with deletion

  • @sammyj29
    @sammyj29 2 года назад +32

    You say inserting / removing from Linked List is 0(1), but for inserting at end, won't you need to traverse the whole list, go to the end and then insert? Wouldn't that make it O(n)? Space complexity would be O(1) since we aren't creating a new linked list.

    • @NeetCode
      @NeetCode  2 года назад +10

      Yeah that's true, while insert/delete anywhere in a linkedlist is O(1), the catch is you will most likely have to traverse it, so overall it will be O(n).

    • @mahanteshambali
      @mahanteshambali 2 года назад +20

      Most implementations use a tail pointer to point to last element which gets updated when inserting at end, making this O(1) for inserting at end.

    • @joakimolovsson7310
      @joakimolovsson7310 2 года назад +3

      @@mahanteshambali most questions I've seen are about singly linked lists

    • @frank3110
      @frank3110 2 года назад +5

      @@joakimolovsson7310 mahantesh is talking about a singly linked list where you have both the head and the tail pointer to have access to the first and the last element in O(1) time.

    • @joakimolovsson7310
      @joakimolovsson7310 2 года назад

      @@frank3110 ah I see! My bad :)

  • @nghiavo6263
    @nghiavo6263 2 года назад +3

    I actually relearn all of data structure in this man. Keep it up

  • @MinorsW
    @MinorsW 2 года назад +22

    Thank you! The summer grind of algorithms starts now!

  • @vipinamar8323
    @vipinamar8323 2 года назад +1

    Technically inserting and deleting from the middle is O(N) even for linked lists, however as articles don't consider indexing time when coming up with the Big O they say it is O(1).

  • @1vader
    @1vader 2 года назад +7

    Pretty sure queues are usually implemented based on arrays. For double ended queues you can use a ring buffer. In most cases, the bad cache locality of linked lists makes them way slower so there are actually relatively few use cases where they really make sense.

    • @theblackunderflow1842
      @theblackunderflow1842 2 года назад

      In JavaScript where adding to the front of the array is O(n) you could benefit from using a linked list where adding to the front is constant time. Given huge problem instances, the locality is less of a problem than the time complexity (at least from my own testing I could be wrong).

    • @1vader
      @1vader 2 года назад

      @@theblackunderflow1842 Adding to the front of a resizable array is expensive in pretty much all languages. But for implementing queues, you just continue from the back when you want to insert before the start. That's what a ring buffer means. It's harder to judge performance in JS without trying it but likely linked lists will still be much worse. Usually, the only situation where linked lists are the best option performance-wise is when you need to do a lot of removing and adding to the middle of the list.

    • @theblackunderflow1842
      @theblackunderflow1842 2 года назад

      @@1vader Sorry, when you said queues I thought you were just referring to standard FIFO queues, with no maximum size or extra logic. I have not tested with double ended or circular queues. I have found that with basic queues, linked list implementation is much quicker than array. But, that wasn't what you were talking about haha that's my bad!

    • @1vader
      @1vader 2 года назад

      @@theblackunderflow1842 For a fifo Q you won't insert at the front though. Or at least you shouldn't. You just use pop() and push() which should be really fast. And you ideally should benchmark stuff like this with a real usage example. Micro-benchmarks which just push or pop a bunch in a row won't give you realistic data on this.

    • @theblackunderflow1842
      @theblackunderflow1842 2 года назад

      @@1vader How would you use push and pop for a Queue? I understand a stack, but I don't understand for a queue. Unless you were trying to implement a queue with two stacks?

  • @echogolf1
    @echogolf1 2 года назад +3

    Absolute next level channel and website. Notifications on, you are doing gods work sir

  • @TheStrafendestroy
    @TheStrafendestroy 2 года назад +14

    I know you have already done so much could you do a series on how to determine time and space complexity

    • @rdothl5
      @rdothl5 Год назад +1

      He doesnt know what he is talking about in this topic, you better look elsewhere.

  • @CEOofTheHood
    @CEOofTheHood 2 года назад +3

    actually, most hashmaps are built on linked lists utilizing chaining.

  • @AlanGCarvajal
    @AlanGCarvajal Год назад +1

    I'm not sure if inserting to a hashmap is, by definition, O(1). It really depends on your hashing function. Your worst case to deal with hashing collisions will be by using a height balanced binary tree as collision structure, so this becomes O((logn)/k). If you never deal with collisions, then yeah O(1) is correct, but you'll need a really big starting array.

  • @tofahub
    @tofahub 2 года назад +17

    You need to traverse the linked list to insert in the middle so it is actually O(n)

    • @seongmoon6483
      @seongmoon6483 2 года назад +3

      My thoughts exactly. Also, removing End would be O(n) IF you did not have a previous pointer.

    • @tofahub
      @tofahub 2 года назад +3

      @@seongmoon6483 you can have a tail pointer even with a singly linked list to remove the last element in constant time

    • @seongmoon6483
      @seongmoon6483 2 года назад

      @@tofahub You need to traverse to the 2nd last node to set that as the new tail and set its next to null

    • @tarekblaugrana1053
      @tarekblaugrana1053 2 года назад

      It seems like he just didn't include the traversal.

    • @tofahub
      @tofahub 2 года назад

      @@seongmoon6483 you make the tail pointer when you first create the linked list just like the head and maintain it every time you insert and remove. That’s O(1)

  • @muhammadjunaid5166
    @muhammadjunaid5166 4 месяца назад

    Removing from the end of a linked list is not O(1), as we need pointer to that node and for that we need to travese through the whole linked list to reach the 2nd last node.

  • @nero9985
    @nero9985 2 года назад +3

    After joining Google, how do you manage personal time with your job?

  • @dbansal1810
    @dbansal1810 2 года назад +3

    Dude best thing about you is you are a good teacher and world have very limited good teachers.

  • @apoorvakhuspe2203
    @apoorvakhuspe2203 3 месяца назад

    Neetcode you're the BEST!!!

  • @unknowncorsairtim
    @unknowncorsairtim Год назад +1

    When inserting/deleting from a linked list, don't you have to get the nodes 6 and 8 before inserting 7? But if you do, insert can't be O(1), since reading from a linked list is O(n)

  • @faysalarab
    @faysalarab 2 года назад +1

    Thanks you, I wish I had a job I would’ve send you more!

  • @AaronJOlson
    @AaronJOlson 2 года назад

    Nice! Very helpful. Until seeing this I thought linked lists were just for making Leetcode questions. Thank you for making this

  • @pratikpatil4880
    @pratikpatil4880 2 года назад +2

    Thank you so much. Can you please make same type of video on top Algorithms.
    Thanks in advance

  • @LokeshPandey-j6k
    @LokeshPandey-j6k Год назад

    Question: May be I am missing something ? But inserting or removing at the mid of linked list how it's O(1) unless we have a reference to the mid node, which in usual scenario we don't have that reference, in that case we have to loop to find the mid node and then insert or remove in this case it won't be constant.

  • @universecode1101
    @universecode1101 2 года назад +2

    Nice 😊 I was searching a similar content ✌🏻

  • @ryan370
    @ryan370 2 года назад +2

    I don't understand the logic for a linked list's time complexity. If it takes O(n) to access an element, how can it take O(1) to insert? Don't we need to access the element i-1 in order to change it's pointer to the new item?

    • @callmeshen9754
      @callmeshen9754 2 года назад

      It's take O(n) to access a specific element because traverse it which means you run from the first node to the last one so in the worse case you will have to run though all the elements which is o(length) = o(n).
      If you want to insert element it will be o(1) only in case you have the pointer to it so you can make a new node and change the pointers from the prev to the new one and the new one into the next.
      BUT in case you need to traverse first (to find the spot which you want to add the new element) will take you o(n) for the traverse and o(1) to insert it to the specific spot which will adds up to o(n) overall.

  • @morenomt27
    @morenomt27 2 года назад

    Just in time sir!!!! Thank you so much!

  • @izikki11499
    @izikki11499 2 года назад

    Super helpful, have tech interview in month, super nervous lol this was very helpful thank you

  • @ahmedmaa4380
    @ahmedmaa4380 2 года назад

    There are concrete like arrays and linked lists and maybe even trees and abstract data structures like graphs and hashtables..

  • @pinkylover911
    @pinkylover911 2 года назад +2

    Hey there thanks so much for your content
    I really need to know the tool u use to draw on the screenshot while illustrating the problems

  • @fuhodev9548
    @fuhodev9548 2 года назад +1

    Hi, I have two questions 1) how about disjoint set, is it important? 2) does an AI Engineer/ AI researcher need a lot of leetcode?

  • @gypsicoder
    @gypsicoder Год назад

    Regarding Linked list how it's possible to insert or remove in middle of the linked list in O(1)? How you will get the node of middle in constant time without storing the index of the nodes of the linked list?

  • @nickold4499
    @nickold4499 2 года назад

    Can you do a video on how to prepare for the Googlyness interview?

  • @johnpill1
    @johnpill1 Год назад

    Fantastic video!

  • @SatishMankar-h9f
    @SatishMankar-h9f Год назад

    @NeetCode , One que, How time complexity of linked list for insert and delete will be O(1), first we need to traverse till that node right ?, which in worst case O(n). so O(n) + O(1) would be O(n) right ?

  • @sealwithawkwardness3951
    @sealwithawkwardness3951 2 года назад +1

    Shouldn’t insertion in between the list about O(n/2) for worst case? Since you have to traverse the list and do the insertion?

  • @hideme420
    @hideme420 2 года назад

    Please answer...
    1)According to you which is best website to practice coding... ?
    2) Companies like Google in thier recruitment process make new ques or they ask from existing ques..?

  • @DodoLP
    @DodoLP 2 года назад

    i would add to hashmap that finding depends on its implementation

  • @randomlyasked
    @randomlyasked 2 года назад

    Thanks for this 😊

  • @chair_smesh
    @chair_smesh 2 года назад

    How do you draw during a google docs whiteboard? Do you have a stylus or do you use a mouse?

  • @rand0md00d
    @rand0md00d 2 года назад +1

    Question:
    Certain languages have built-in min/max heap and other DS libraries that you can call upon if you recognize them as a useful tool for solving a problem. However, other languages don't (and unluckily, my preferred language doesn't)
    During interviews, is it kosher to at least have the class implementation and methods saved somewhere you can copy paste in, since the interviewer probably cares more that you recognized when and how to use them? Mostly because I can explain and write out a Trie's basic methods in a few minutes, but to implement a heap from scratch will take way too much time.

  • @niharika7040
    @niharika7040 2 года назад

    Could you please make a video on leet code problem "1192. Critical Connections in a Network"

  • @AlexN2022
    @AlexN2022 2 года назад

    why are hash maps said to have O(1) key search/access/insert time, when conflicts exist? Is there an implementation that works in O(1) in presence of conflicts?
    I trust STL to be well implemented, and STL guarantees O(lg) for sorted and O(N) for unordered maps/sets.

  • @ericleijonmarck
    @ericleijonmarck 2 года назад

    hey, what is the material that you use for creating these videos? that tools are you using?

  • @smirkedShoe
    @smirkedShoe 2 года назад

    @NeetCode, could you please make a video or attach some links to help understand the complexity analysis of Backtracking problems ? For me, calculating the TC of backtracking problems is more tough than actually coding the solution.

  • @studyaccount794
    @studyaccount794 2 года назад

    Please do a video on Leetcode 907. Sum of subarray minimums.

  • @saifparkar5410
    @saifparkar5410 2 года назад +2

    Hey neet can you please release a ds and algos course for python since there aren't any good ones out there

  • @kirillzlobin7135
    @kirillzlobin7135 4 месяца назад

    Why hen we insert in the middle of linked list - it is not considered as O(n)? We need to traverse LL to the position where we want to insert an element

  • @gokusaiyan1128
    @gokusaiyan1128 2 года назад

    What software do you use to write on the video ? you know whiteboard thing that you do in videos

  • @alessandrolodi8951
    @alessandrolodi8951 2 года назад

    fantastic one

  • @vectoralphaSec
    @vectoralphaSec 2 года назад +1

    Is this needed for ALL interview like entry level or junior positions or just top tech FAANG/MAANG like companies??

  • @friction5001
    @friction5001 2 года назад +2

    Neet, what is a good use case for each data structure?

  • @showbikshowmma3520
    @showbikshowmma3520 2 года назад

    I love the linked list😍😁

  • @nandinirm2234
    @nandinirm2234 2 года назад

    Please make a video on time complexity Big O using python

  • @hyunsooting
    @hyunsooting 2 года назад

    Thanks!

  • @FatalDistractions
    @FatalDistractions 2 года назад

    Removing a node at the end is not constant. Not in a single linked list

  • @rafald5097
    @rafald5097 2 года назад

    Hey!
    Can you recommend and system design site/source?

  • @bartumumcu2936
    @bartumumcu2936 Год назад

    As far as I know, inserting an element at any position to a linked list is O(N) because you have to traverse and update your pointer untill that element. In the video it says O(1). How is this possible?

    • @dennislam149
      @dennislam149 Год назад

      I think he means inserting at the front or back but other wise it is O(n)

  • @nurhusni
    @nurhusni 2 года назад

    3:58 i've never understood why inserting & deleting the middle of the linked list is O(1).
    how does it suddenly find the pointer to the middle element?
    shouldn't it be O(n) because you still need to iterate from the first element to the n-th element if you wanna insert or remove it?

    • @NeetCode
      @NeetCode  2 года назад +1

      Technically it's O(1) when you insert or remove, but yes, practically it's O(n) because you have to iterate to the position. But with arrays, it's O(n) regardless
      This distinction can be important sometimes, like in the LRU cache problem

  • @evelynsummer4020
    @evelynsummer4020 2 года назад

    Hi neetcode can you suggest best resources to study linked list?

  • @dzheliezniak
    @dzheliezniak 2 года назад

    wouldn't for linked list insert and remove middle still be O(n) because you need to spend time navigating to that element?

  • @thomasthereal4067
    @thomasthereal4067 Год назад

    Is trhere any situation, where an array is faster or more efficient than a hashmap?

  • @biswaMastAadmi
    @biswaMastAadmi 2 года назад

    loved it

  • @rajatarora8105
    @rajatarora8105 2 года назад

    I have a request for a leetcode problem Can you solve 1079. Letter Tile Possibilities

  • @myrtiy
    @myrtiy 2 года назад

    Do you have a prepfully account? Can we contact you for a practice swe intern interview?

  • @yang5843
    @yang5843 2 года назад +4

    By the time you read this comment, I'll be in FAANG.

  • @Verysheyn
    @Verysheyn 2 года назад

    what app do you use for whiteboard?

  • @urdarkside1
    @urdarkside1 2 года назад

    Hey NeetCode, Can you recommend a free interactive source to learn the Big O Theory?

    • @yogeshmotiyani3535
      @yogeshmotiyani3535 2 года назад

      Check inside code free course on Big O notation

    • @urdarkside1
      @urdarkside1 2 года назад

      @@yogeshmotiyani3535 Can you kindly drop a link for that please

    • @varunshrivastava2706
      @varunshrivastava2706 2 года назад

      Search Kunal Kushwaha on youtube. You will find his video on Time Complexity.

  • @pokerbuddy62
    @pokerbuddy62 2 года назад

    I thought arrays were immutable in size?

  • @MOHITRANA-to7rf
    @MOHITRANA-to7rf 2 года назад

    i try to maintain consistency but getting failing. what should i focus on ds or develpment.

    • @djudsod959
      @djudsod959 2 года назад +5

      Dsa practice helps when you start development . Not directly but logic starts clicking faster. So DSA first.

    • @vaibhavnayak3416
      @vaibhavnayak3416 2 года назад

      I am doing both. It is a good way imo.

    • @ihsannuruliman3656
      @ihsannuruliman3656 2 года назад

      do both, sometimes yeah I also too get too indulged on one side, but after some times I'm able to balance between them.

  • @derek.morrison
    @derek.morrison 2 года назад

    Please, ease up on the embedded ads. There are three of them here (three!). I pay for RUclips Premium.

  • @deeplearningpartnership
    @deeplearningpartnership Год назад

    Cool.

  • @debendranathmukherjee5721
    @debendranathmukherjee5721 2 года назад

    DUDE... its a request plz plz plz either get a better microphone or speak a bit louder... haha... im literally playing it on Loudspeakers yet ur vids sound like a squeak...

  • @NinjiaJoeLife
    @NinjiaJoeLife 2 года назад

    first comment left by me

  • @tuanva6484
    @tuanva6484 Месяц назад

    Stack is used more than linkedlist

  • @YsOsEriOuz760
    @YsOsEriOuz760 2 года назад

    literally my uni course in 13min.

  • @free-palestine000
    @free-palestine000 2 года назад +1

    day 10 of asking neet to make a video on how to communicate during coding interviews 🥲