This is the Most Asked FAANG Interview Question! - Two Sum - Leetcode 1

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 มี.ค. 2024
  • FAANG Coding Interviews / Data Structures and Algorithms / Leetcode

ความคิดเห็น • 107

  • @GregHogg
    @GregHogg  3 หลายเดือนก่อน +14

    I really don't know why FAANG loves Two Sum, Three Sum and so on so much... but they do! So here you go! :)

    • @abhiyans5510
      @abhiyans5510 3 หลายเดือนก่อน +1

      Is Threesum and so on... also best solved with hashmaps?

    • @oferron4894
      @oferron4894 3 หลายเดือนก่อน +2

      You disrespect FAANG coding questions, and then fail them.. What happens if you are asked for a target which is twice an existing number in the array? What happens if you have duplicates in the array? Consider [2, 3] and [2, 2, 3] with the target 4.

  • @JohnWasinger
    @JohnWasinger 3 หลายเดือนก่อน +149

    Interviewer: How do you…
    Applicant: HASHMAP!

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน +6

      So true

    • @chamuelcastillo6410
      @chamuelcastillo6410 3 หลายเดือนก่อน

      😂

    • @Leonhart_93
      @Leonhart_93 3 หลายเดือนก่อน +1

      That's because code speed and time complexity far more important in general than some extra memory usage. Even a few dozen GBs of extra RAM are quite cheap compared to a slightly better CPU.
      For example no one complains that a game occupies 10 GB of ram as long as it runs fast.

  • @euclid9492
    @euclid9492 3 หลายเดือนก่อน +12

    I think you can simplify this to a single pass through the array. As you go through the first time, check if the complement of the current value is already loaded in the map. If so return that and the current index. If not, add the current value to the map and continue iterating. This way you’re essentially checking each number against all numbers behind it. Still the same bigO, but you cut the iterations in half.

  • @mastrassassn5811
    @mastrassassn5811 3 หลายเดือนก่อน +9

    How in the fuck did I get into the Einstein side of TH-cam shorts

  • @rony86
    @rony86 3 หลายเดือนก่อน +3

    its a dynamic programming approach called sum of subsets

  • @nithish5296
    @nithish5296 2 หลายเดือนก่อน

    It would take your tc:0(nlogn) and sc:0(n) but if your sort it and implement 2pointer algo it takes constant space complexity with time complexity beeing O(nlogn)

  • @hit7090
    @hit7090 16 วันที่ผ่านมา

    Best part about this one is that you don’t need to sort the array for getting all the pairs😊

  • @sruthimajhi5610
    @sruthimajhi5610 17 วันที่ผ่านมา

    you can use the two pointer approach here for the most optimal solution with time complexity - O(n) and constant space complexity

  • @peacist5098
    @peacist5098 3 หลายเดือนก่อน +2

    I would have used two pointers for a sorted array, btw unlocked new jutsu😅

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน

      Hahaha awesome!

    • @user-ub9pd5yl7p
      @user-ub9pd5yl7p 2 หลายเดือนก่อน

      but this wont use if you question asked to return an index too.

  • @Rajib317
    @Rajib317 3 หลายเดือนก่อน

    Ideally if the array is sorted the you should use binery search where the complexity is Log n. But in other cases your method is optimum.

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน +3

      Actually if it's sorted the best you can do is still O(n)

  • @chrisgascoyne2958
    @chrisgascoyne2958 3 หลายเดือนก่อน +3

    I used to get really confused when people would talk about 'hashmaps', since I learned everything in PHP and JS. Felt like such an idiot when I realised it's just an associative array 🤦‍♂️

    • @jvanb231
      @jvanb231 3 หลายเดือนก่อน

      and people like to call indices pointers -- not awesome when you are used to pointers referring to memory addresses.

    • @Leonhart_93
      @Leonhart_93 3 หลายเดือนก่อน

      Yeah, the quintessential JS object that holds pretty much everything.

  • @arsheyajain7055
    @arsheyajain7055 3 หลายเดือนก่อน

    Awesome 👏

  • @GregHogg
    @GregHogg  3 หลายเดือนก่อน

    By the way, sessions are filling up fast for my 1 on 1 tutoring sessions! Email greg.hogg1@outlook.com if you're interested in learning DSA with me :)

  • @thezenmasterandy9792
    @thezenmasterandy9792 3 หลายเดือนก่อน +2

    But what happens if the number you're at is half the target, so when searching if there's a distinct index with that value, your program might just use your current index? For example, if you're at a 3 and your target is 6, you'd have to check if there's another 3 rather than just say your current 3 is in the array (e.g arr = [3,2,1] and target is 6).

    • @mlgswagman6002
      @mlgswagman6002 3 หลายเดือนก่อน +1

      when ur at an element, update the hashmap AFTER u look up target - element

    • @urbsrocher
      @urbsrocher 3 หลายเดือนก่อน

      There should be a check for where desired == target. If so, h[desired] needs to be an array with length > 1, and return its first two elements

  • @tashi7160
    @tashi7160 3 หลายเดือนก่อน

    I thought you’re gonna come with some other solutions, just for fun. Same old same old

  • @derabidian
    @derabidian 3 หลายเดือนก่อน

    Nice

  • @anti-dn541
    @anti-dn541 2 หลายเดือนก่อน

    this is whrere we begin

    • @GregHogg
      @GregHogg  2 หลายเดือนก่อน

      Yes 💯🤣

  • @acceleratedofficial1576
    @acceleratedofficial1576 2 หลายเดือนก่อน

    What about if the target is 4 and your elem at which u are is 2 ?
    What if there is more than one Occurance of any number in list and then how will you store the no into dict/hashmap

    • @GregHogg
      @GregHogg  2 หลายเดือนก่อน +1

      Correct! You'd need to make sure that the index in the Hashmap isn't the same index as where you are in the array. Awesome thinking :)

  • @rosankumarsenapati3510
    @rosankumarsenapati3510 3 หลายเดือนก่อน +10

    Best solution will be 2 pointer approach

    • @ElephantSoup
      @ElephantSoup 3 หลายเดือนก่อน +6

      You would need to pre-sort the list for this approach to work

    • @thecoolnewsguy
      @thecoolnewsguy 28 วันที่ผ่านมา +1

      Unless the array is sorted, no.

  • @hlubradio2318
    @hlubradio2318 3 หลายเดือนก่อน

    Did it

  • @yasserhussain6272
    @yasserhussain6272 วันที่ผ่านมา

    why not just .index(7) instead of storing a hashmap

  • @jo-fe9mb
    @jo-fe9mb 3 หลายเดือนก่อน

    still on2 because you are doing 2 loops there

  • @sarveshnagre4223
    @sarveshnagre4223 2 หลายเดือนก่อน

    Hashmap & Two pointers 🗿🗿

    • @Momentum_animation
      @Momentum_animation หลายเดือนก่อน

      The one he's using is it brute force approach?since I'm seeing 2for loops nested loop

    • @thecoolnewsguy
      @thecoolnewsguy 28 วันที่ผ่านมา

      ​@@Momentum_animationyes

  • @observer3987
    @observer3987 3 หลายเดือนก่อน +2

    Is everything solved with hashmap? or is it my recommendation.

    • @gustavohonorato147
      @gustavohonorato147 3 หลายเดือนก่อน +1

      because is ordered, you could solve this with two pointers two

    • @ZillasWay
      @ZillasWay 3 หลายเดือนก่อน +1

      Just throw in a hashmap in every problem and you basically solved 80% of it

  • @khaledalmutairi2604
    @khaledalmutairi2604 3 หลายเดือนก่อน +1

    Lets say there is 3 instead of 7 in the map , and there also a 6 somewhere in it, will the code run first for 9-2 and when it does not find 7 it loop again until it searches for 9-3 to then find the 6?

    • @JustinK0
      @JustinK0 3 หลายเดือนก่อน

      There is no "loop again" the solution is O(n)

    • @dannyisrael
      @dannyisrael 3 หลายเดือนก่อน

      It will iterate once through the hashmap looking for a possible solution

    • @khaledalmutairi2604
      @khaledalmutairi2604 3 หลายเดือนก่อน

      @JustinK0 it subtract the target from the first element and see if there is a match to the result (of the subtraction) but if it did not find a match will it not perform the subtraction on the second element and so on ...? I hope I explained my request for clarification clearly and thank you for the reply

    • @khaledalmutairi2604
      @khaledalmutairi2604 3 หลายเดือนก่อน

      @@dannyisrael it subtract the target from the first element and see if there is a match to the result (of the subtraction) but if it did not find a match will it not perform the subtraction on the second element and so on ...? I hope I explained my request for clarification clearly and thank you for the reply

    • @victortang9320
      @victortang9320 3 หลายเดือนก่อน

      If you don’t understand how a hashmap works idk man

  • @tengisganbaatara
    @tengisganbaatara 3 หลายเดือนก่อน

    Brain boom

  • @kotXbit
    @kotXbit 2 หลายเดือนก่อน

    I tried to answer this simple question in c#. Messed up the declaration of the dictionary and my streak. C# outside of unity is 😢.

    • @GregHogg
      @GregHogg  2 หลายเดือนก่อน

      Yeah I also had an easier time with it in unity

  • @nitishkunche2334
    @nitishkunche2334 3 หลายเดือนก่อน +2

    We can also use binary search

    • @M4AH1990
      @M4AH1990 3 หลายเดือนก่อน +5

      Probably no guarantee that the array is given out sorted.
      Also, even if sorted, it would be o(n.lg(n)).

    • @thecoolnewsguy
      @thecoolnewsguy 28 วันที่ผ่านมา

      ​@@M4AH1990 ​I don't think so. Binary search takes log(n) in the worst case scenario not n.log(n) because n.log(n) means that we have two loops with one being nested like this (that's just an example):
      for (int i = 0; i < n; i++)
      --for (int j = 1; j < n; j *= 2)
      Usually you find this complexity in divide and conquer problems. Binary search only requires one loop so it's log(n) not n.log(n) (assuming the array is sorted)

    • @M4AH1990
      @M4AH1990 27 วันที่ผ่านมา

      @@thecoolnewsguy It's o(n.lg(n)) because, in the worst scenario, you'd go over each element from the start, and then search for its answer in the remaining of the array. Of course, assuming it's sorted.
      Well, it could be lower. Because if you're at index i, as i goes from 0 to n - 2, you'll perform a binary search in (n - i - 1) elements. Not sure about the math here, but it won't be as low as o(n), as I explained above.

    • @thecoolnewsguy
      @thecoolnewsguy 27 วันที่ผ่านมา +1

      @@M4AH1990 oh I got you. Yes you're right it's O(n.log(n)) because you will need to iterate through the whole array O(n) in the worst case scenario and then for each element, you perform binary search O(log(n)) so there's a nested loop and the final complexity is indeed O(n.log(n)). Forgive me for my mistake 😁.

    • @M4AH1990
      @M4AH1990 27 วันที่ผ่านมา

      @@thecoolnewsguy You got it bro 😎

  • @adventureswithpasa
    @adventureswithpasa 3 หลายเดือนก่อน

    do this on c then we will talk.

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน

      I think it will be awhile until we talk then haha

  • @oferron4894
    @oferron4894 3 หลายเดือนก่อน +16

    You disrespect FAANG coding questions, and then fail them.. What happens if you are asked for a target which is twice an existing number in the array? What happens if you have duplicates in the array? Consider [2, 3] and [2, 2, 3] with the target 4.

    • @victortang9320
      @victortang9320 3 หลายเดือนก่อน +4

      I think I would just use the hashmap to find any values before implementing there current value into the hashmap, I.e check for 2 before I implement 2 into the hashmap. Chill out man, part of the journey with the interview is to find alternate solutions to fulfill the criteria the interviewer wants.

    • @oferron4894
      @oferron4894 3 หลายเดือนก่อน +1

      @@victortang9320 I didn't like his disrespectful comment "I really don't know why FAANG love this question so much, but they do".

    • @lafireteamplx3400
      @lafireteamplx3400 3 หลายเดือนก่อน

      ​@@oferron4894 surely when they see your comments praising their questions they will hire you as their new ceo

    • @M4AH1990
      @M4AH1990 3 หลายเดือนก่อน +1

      Probably he said it as a video starter.

    • @M4AH1990
      @M4AH1990 3 หลายเดือนก่อน +1

      And since you got me interested, I’d say the case of duplicate values would still be handled with the same solution. If the target is 10, and the current iteration is on a 5, then I’d simply look if the map has a 5 already.

  • @fortunethedev
    @fortunethedev 3 หลายเดือนก่อน

    Couldn’t you just use 2 pointers for this and use them to traverse the array, and removing the extra space complexity of the hashmap?

    • @victortang9320
      @victortang9320 3 หลายเดือนก่อน +2

      But you increase time complexity since it it wouldn’t be a hashmap solution anymore. I think most people can agree that the current model of computing pursues speed and doesn’t care about space complexity.

  • @masteradvisor594
    @masteradvisor594 3 หลายเดือนก่อน

    Wont it iterate whole hashmap to search for that number internally? Just curious

    • @absentchronicler9063
      @absentchronicler9063 3 หลายเดือนก่อน

      No because searching in a hashmap is O(1)

    • @masteradvisor594
      @masteradvisor594 3 หลายเดือนก่อน

      @@absentchronicler9063 ok got it.

  • @metinEsturb
    @metinEsturb 3 หลายเดือนก่อน +1

    I love this comedian :)

  • @gast128
    @gast128 3 หลายเดือนก่อน

    Not sure about this. Building a hash map isn't actually free either.

  • @rileycorzine2985
    @rileycorzine2985 3 หลายเดือนก่อน

    the n should be a different company

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน

      Sorry?

  • @ashishkumarsingh6618
    @ashishkumarsingh6618 3 หลายเดือนก่อน

    This can be solved in logn

  • @destroyer-fr4dz
    @destroyer-fr4dz 3 หลายเดือนก่อน

    How do you fuck up the solution to the easiest leetcode question? Bruh you make a set and add to it as you iterate through. Its one pass you don’t need to make a hashmap before hand

    • @mujtabarehman5255
      @mujtabarehman5255 3 หลายเดือนก่อน

      Since you have to return the indices, how would you do it with a set? Unless you are being imprecise with your language and really mean a hashmap.
      Also, there's obviously a trade off between readable code for someone seeing this problem for the first time, and the ideal answer you'd want to to give in an interview. You're acting like he gave a O(2^n) solution for this problem. It's literally fine.

    • @destroyer-fr4dz
      @destroyer-fr4dz 3 หลายเดือนก่อน

      ⁠​⁠@@mujtabarehman5255Yea, I mean. Hashmap. Add to it as you iterate through.
      If he is trying to be educational and teach others, he should not show a subpar technique for the easiest leetcode problem. It it was something more complicated I would understand

    • @mujtabarehman5255
      @mujtabarehman5255 3 หลายเดือนก่อน +1

      @@destroyer-fr4dz Yeah, fair enough then.
      I suppose at this point if a company gives you TwoSum then you pretty much have to nail it, given how popular it is. I still think that getting the main idea (ie use a hashmap) across is more important than the ideal code. But on the flip side you can argue that the single loop isn't even much harder, so he should easily be able to show that instead. I get your point.

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน

      Just trying to make it readable. You're right, there's slightly better

  • @Nemesis-db8fl
    @Nemesis-db8fl 3 หลายเดือนก่อน

    How is this solution O(n)? If anything i think it O(2n)

    • @L8NightLearning
      @L8NightLearning 3 หลายเดือนก่อน

      pages.cs.wisc.edu/~vernon/cs367/notes/3.COMPLEXITY.html When calculating Big-O Notation, its customary to drop the factor for each term since they will even out if the input is large enough, so O(2n) gets reduced to O(n) approximately.

    • @nolanjones1027
      @nolanjones1027 3 หลายเดือนก่อน +1

      O(2n) is O(n) time complexity

  • @usemayonaise
    @usemayonaise 3 หลายเดือนก่อน +1

    Lol this guy and his junk code again. You can easily do this in a single loop by checking for the value in the map and adding only if its not there. These kind of details matter when interviewing esp when its something so obvious

  • @tjscorp
    @tjscorp 3 หลายเดือนก่อน

    This is explained so badly, I couldn't understand the solution even though I already know it 😢

    • @arsheyajain7055
      @arsheyajain7055 หลายเดือนก่อน

      Something’s really wrong with your head then.

  • @Btstaz
    @Btstaz 3 หลายเดือนก่อน

    the fact companies hire people based on these stupid problems boggles the mind.

    • @stoppls1709
      @stoppls1709 3 หลายเดือนก่อน

      this won't ever be asked anyway, they actually ask leetcode hards

  • @HR-pz7ts
    @HR-pz7ts 3 หลายเดือนก่อน

    If time isn't a constraint then we could use two pointers if the array is sorted which probably takes nlogn time

    • @JustinK0
      @JustinK0 3 หลายเดือนก่อน

      Why? Hashmap Is faster at O(n)

  • @jdhouse6501
    @jdhouse6501 3 หลายเดือนก่อน

    This solution is O(n^2) lol

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน +1

      It's not lol

    • @jdhouse6501
      @jdhouse6501 3 หลายเดือนก่อน

      ​@@GregHoggOh shit you're right. I didnt realize it was O(1) to check membership in a hashmap. That's cool

  • @logiclrd
    @logiclrd 3 หลายเดือนก่อน

    Building the hash map is not O(n). If you don't already have the map, then your solution isn't O(n).

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน +9

      Well this is just wrong lol

    • @Ryan-tb7pp
      @Ryan-tb7pp 3 หลายเดือนก่อน +1

      @@GregHoggu tell him greg u a legend

    • @PiotrPilinko
      @PiotrPilinko 3 หลายเดือนก่อน +2

      Expected complexity of a hashmap build IS O(n). Pessimistic is O(n^2) - but this happens only when you have a very bad hashing function with a lot of collisions.

    • @logiclrd
      @logiclrd 3 หลายเดือนก่อน

      It's only O(n) if you have _no_ collisions. Average time is n lg n. Best possible case is n, but worst possible case is n^2.

    • @olehsolomka8268
      @olehsolomka8268 3 หลายเดือนก่อน

      Even if there where 10 different loops , it’s still o(n)
      If loop is inside a loop than it’s o(n^2)