LeetCode 721. Accounts Merge [Coding Interview Solution Explained]

Sdílet
Vložit
  • čas přidán 25. 07. 2024
  • In this video I explain the solution of a Facebook coding interview question “Accounts Merge” [LeetCode 721].
    You can find it here -
    leetcode.com/problems/account...
    This question is frequently asked in Google and Facebook software engineering interviews (according to LeetCode)
    The code for this solution is here -
    If there is a coding interview problem you would like me to cover, let me know in the comments.

Komentáře • 89

  • @austin4855
    @austin4855 Před 2 lety +15

    I was doing well on the Grind75 list until this problem just crushed my confidence. I don't think I've ever used union find and wouldn't have recognized it, and the graph approach I was attempting had me going in circles. Thanks for the excellent explanation!

    • @ShiranAfergan
      @ShiranAfergan  Před 2 lety +3

      Yes union find is not super common but it does come up every now and then :) I’m glad the video helped!

  • @MrMakemusicmike
    @MrMakemusicmike Před 3 lety +6

    This video really helped me with this Leetcode problem.
    I probably didn't code exactly the way you did, but I have a solution beating 61.97% of submissions and better than 93.09% of solutions in memory usage.
    I think using lambda helped a lot for removing duplicates in the accounts that did not need to be merged.
    Thanks a ton!

  • @harshranjankumar964
    @harshranjankumar964 Před 2 lety +4

    The explanation I was looking for!!!! ❤️❤️

  • @SumitKumar-fn3gj
    @SumitKumar-fn3gj Před 3 lety +2

    Thank you very much for this video. You teach very well. I have a hard time solving this but your way of teaching makes me understand very well and I solve it my own without seeing code of yours. Again thank you very much.

  • @ritwik121
    @ritwik121 Před 3 lety +5

    Great explanation. Thanks

  • @its_chirantan
    @its_chirantan Před 2 lety +1

    I was struggling a lot with this problem. Thanks for the explanation.

  • @anshumansinghrajawat7242
    @anshumansinghrajawat7242 Před 2 lety +1

    Nice explanation Shiran, now i am feeling more confident in DSU, thanks!

    • @ShiranAfergan
      @ShiranAfergan  Před 2 lety +1

      That’s awesome 👏🏼 👏🏼 glad it helped :)

  • @SeemantAggarwal
    @SeemantAggarwal Před 3 lety +4

    Good explanation. It'll be more helpful if you could add comments to your code. Thanks for the great effort though.

  • @lgodlike2324
    @lgodlike2324 Před rokem

    Valuable video ! Thank you very much !

  • @akashjain5931
    @akashjain5931 Před 2 lety +1

    Very well explained ! You earned a subscriber. Cheers!

  • @akashjaiswal6478
    @akashjaiswal6478 Před 2 lety +2

    The way you write code is amazing, please upload a video on what to keep in mind before writing a code and how to write an efficient code like you

    • @ShiranAfergan
      @ShiranAfergan  Před 2 lety

      This type of video is definitely on my list :) thanks 😊

  • @aditisrivastava6499
    @aditisrivastava6499 Před 2 lety

    Very well explained. Thanks a lot.

  • @tntcaptain9
    @tntcaptain9 Před 3 lety +2

    Thanks for the solution. One point is that if you use ordered map for email2AccountIdx, you wouldn't need to sort the vector in the end.

    • @ShiranAfergan
      @ShiranAfergan  Před 3 lety

      You’re right :) You could do that. Then the first loop will take longer because insert to map is log(n) vs insert to hash table which is average constant. But you’re avoiding the sort in the end. Complexity will be the same but if there are many duplicate emails, it will probably be better to use hash and sort at the end. Because you’re sorting less elements than what you insert to the table. What do you think?

  • @sakshamkapoor5905
    @sakshamkapoor5905 Před 3 lety +2

    Amazing explanation, thanks!

  • @lmnefg121
    @lmnefg121 Před 2 lety +1

    I just looooove your lecture~!

  • @andriidanylov9453
    @andriidanylov9453 Před rokem

    Impressive. Thanks

  • @musicperson94z
    @musicperson94z Před 3 lety +6

    Great video. Can you also make a video on how you learn all the algorithms or prepare interview questions in general?

    • @ShiranAfergan
      @ShiranAfergan  Před 3 lety +2

      Thanks Jiadong! That’s a good idea, I’ll try to think on this type of video

    • @saiashok28
      @saiashok28 Před rokem

      @@ShiranAfergan I am searching for the same in your play list too. I like the Data Structure. Would be also helpful if you do one Data Structure with simple examples programmatically. I know too much, but would help even beginners.

  • @jlecampana
    @jlecampana Před 2 lety +2

    Hello Shiran Nice video. It's still such a contrived solution for this problem, it feels like without having seen the problem before you would naturally go for some kind of DFS approach though.

    • @ShiranAfergan
      @ShiranAfergan  Před 2 lety +3

      I see what you mean, and you could do dfs here. The complexity will be similar so it’s still a good solution but in practice it will be slower because you would have to construct the graph

    • @jlecampana
      @jlecampana Před 2 lety

      @@ShiranAfergan Thank you for your reply Shiran! I have noticed that Union-Find it's a such a prevalent topic for interviews (or at least in coding practice platforms like LeetCode it is), but do you know if Union-Find is common enough so that a Question that REQUIRES it pops up in a FAANG interview? I'm not sure if I should invest my time in learning it fully is worth it? or I could do well with Graph traversals alone. What do you think? Also, I'd be glad to be able to connect with you on Linkedin, hope it's possible. Take care and thank you for these awesome videos, keep it up!

    • @jlecampana
      @jlecampana Před 2 lety

      @@ShiranAfergan You're really great at explaining tough Interview Questions, Would you mind doing LeetCode 809 - Expressive Words next? That one is trickier than it looks IMHO and Google asks it during interviews. Have a nice week!

    • @ShiranAfergan
      @ShiranAfergan  Před 2 lety +2

      I don’t think union-find should be your top priority. Graphs are more important. But I would at least learn the basics of it - which operations it can do, what’s the time complexity and basic implementation.
      Sure, we can connect on LinkedIn, send me an invite.
      Thanks for the suggestion (LeetCode 809) I will look into this :)

    • @jlecampana
      @jlecampana Před 2 lety

      @@ShiranAfergan Just sent you a Linkedin connection request, I hadn't seen that you said I could do it, Thank you Shiran!

  • @gabrieldjebbar7098
    @gabrieldjebbar7098 Před 3 lety +3

    Nice video Shiran :) You can also solve this using a graph with dfs (it's easier when you have forgotten how dsu works exactly 😂)

    • @ShiranAfergan
      @ShiranAfergan  Před 3 lety +1

      Thanks Gabriel, good point! I also talked about it in the video but removed it cause it was too long 😄 i was saying that DFS will take longer because you have to construct the graph but will have similar complexity so it’s also a good solution

    • @gabrieldjebbar7098
      @gabrieldjebbar7098 Před 3 lety +1

      @@ShiranAfergan Hi Shiran, you should check out this question : "Find Median from Data Stream". The solution is very interesting, also it seems to be a popular interview question this days :)

  • @tonystarc9567
    @tonystarc9567 Před 3 lety +1

    Great. Could you please make some more videos on problems related to union find . Any problem on leetcode that would be good to grasp the concept better.

    • @ShiranAfergan
      @ShiranAfergan  Před 3 lety

      I have another union-find problem on my channel. check out “most stones removed with same row or column”
      czcams.com/video/beOCN7G4h-M/video.html

  • @satyajitmahunta7906
    @satyajitmahunta7906 Před 2 lety +2

    Simply Awesome :)

  • @journeytowardslife9830
    @journeytowardslife9830 Před 2 lety +1

    Amazing !

  • @akbar55555shaikh
    @akbar55555shaikh Před 2 lety +1

    Can you please create video on union-find data structure?

  • @kuangyimeng8850
    @kuangyimeng8850 Před 2 lety +1

    good video! Help me a lot!

  • @morzamir5197
    @morzamir5197 Před 2 lety +1

    Genius!!

  • @kshitijdubey8994
    @kshitijdubey8994 Před 3 lety +2

    thanks :)

  • @KeimaTFT
    @KeimaTFT Před 3 lety +1

    nice video! could i ask what tool you are using to draw on your screen?

    • @ShiranAfergan
      @ShiranAfergan  Před 3 lety

      Thanks :) it’s an ipad pro and the app is GoodNotes

  • @henz103
    @henz103 Před 2 lety

    This problem is very implementation heavy

  • @arnabkhan95
    @arnabkhan95 Před 2 lety +1

    Thanks for the solution, Could you please make a video on 552. Student Attendance Record II problem?

    • @ShiranAfergan
      @ShiranAfergan  Před 2 lety +1

      Thanks for the suggestion! I’ll look into it

  • @protyaybanerjee5051
    @protyaybanerjee5051 Před 3 lety +1

    Can you share your screen writing setup ? Looks great

    • @ShiranAfergan
      @ShiranAfergan  Před 3 lety

      Thanks :) It’s an iPad Pro and the app is GoodNotes

  • @himanshushekhar5875
    @himanshushekhar5875 Před 2 lety +2

    Thanks

  • @vanshsehgal3330
    @vanshsehgal3330 Před 2 lety +1

    "After a million compilation errors" - Yeah totally relatable. Damn!!😂😂

  • @shanthureddy4234
    @shanthureddy4234 Před 2 lety +1

    c++ ? I wish it was in Java or C#
    but thanks for the preface it is amazing and clear

    • @ShiranAfergan
      @ShiranAfergan  Před 2 lety +1

      Haha Cpp is my favorite language 🤓. Glad you liked the non coding parts 😆

  • @RahulSingh-vv9jj
    @RahulSingh-vv9jj Před 3 lety +2

    I am new to coding can you explain line no 6 , UnionFind(int n): groupcount(n){ /code here}, I didn't understand the ->:groupcount(n) , how we are using constructor.

    • @ShiranAfergan
      @ShiranAfergan  Před 3 lety +1

      Hey Rahul, this line initializes the groupCount variable to n.

  • @saisidharthpatlolla3542
    @saisidharthpatlolla3542 Před 3 lety +1

    May I know why you are storing input in the address variables first?

    • @ShiranAfergan
      @ShiranAfergan  Před 3 lety

      Hey Sai, which address variables? Which line of code are you referring to specifically?

  • @prudhvipadala3603
    @prudhvipadala3603 Před 2 lety +1

    UnionFind(int n):groupCount(n) {} can any please explain what this part signifies?
    i know constructors but i dont understand why colon and groupCount variable given after constructor

    • @ShiranAfergan
      @ShiranAfergan  Před 2 lety

      This type of syntax is used to initialize the data members of a class. That specific line initializes “groupCount” to n. You can look up “CPP Initializer List” for more information.

    • @prudhvipadala3603
      @prudhvipadala3603 Před 2 lety +1

      @@ShiranAfergan oh ok thank you so much

  • @nipunshah1373
    @nipunshah1373 Před rokem

    Why you are not updating rank when either of merging group has lesser rank ?

  • @akashjaiswal6478
    @akashjaiswal6478 Před 2 lety +1

    Please make a video on cheapest flight within k stops

    • @ShiranAfergan
      @ShiranAfergan  Před 2 lety

      Thanks for the suggestion! I’ll look into it :)

  • @yashs6586
    @yashs6586 Před 2 lety

    Code for the solution?

  • @yash.dwivedi
    @yash.dwivedi Před 2 lety +1

    which software do you use to write on the screen?

  • @tombrady7390
    @tombrady7390 Před 2 lety +1

    Leetcode 862 shortest subarray with sum atleast K if u get time please make video on it

  • @Sksahu_123
    @Sksahu_123 Před 3 lety +1

    vgood

  • @RahulKashyap-yv5ox
    @RahulKashyap-yv5ox Před 2 lety

    Leetcode 1631
    Path With Minimum Effort:Mam can you make video on this question .

  • @keerthivasan138
    @keerthivasan138 Před rokem

    Mam kindly do leetcode 862

  • @prashantsahu6212
    @prashantsahu6212 Před 2 lety

    Indians are everywhere

  • @uptonogood300
    @uptonogood300 Před 2 lety

    this problem should be labeled hard ffs

  • @AyushRaj-pm1dz
    @AyushRaj-pm1dz Před 2 lety +1

    Same C++ Code :
    class UnionFind{
    private:
    vector parent;
    vector ranks;
    public:
    int groupCount;
    UnionFind(int n){
    groupCount = n;
    parent.resize(n);
    ranks.resize(n);
    }
    void intialize(int x){
    parent[x] = x;
    }
    int find(int x){
    if(x!=parent[x])
    parent[x] = find(parent[x]);
    return parent[x];
    }
    void unify(int x,int y){
    int rootX = find(x);
    int rootY = find(y);
    if(rootX==rootY) return;
    groupCount--;
    if(ranks[rootX]>ranks[rootY])
    parent[rootY] = rootX;
    else{
    parent[rootX] = rootY;
    if(ranks[rootX]==ranks[rootY])
    ranks[rootY]++;
    }
    }
    };
    class Solution {
    public:
    vector accountsMerge(vector& accounts) {
    int n = accounts.size();
    UnionFind uf(n);

    unordered_map mail2accountIdx;
    for(int i=0;isecond].push_back(email);
    }
    }

    for(int i=0;i

  • @annsway
    @annsway Před rokem

    Hi, Thank you for your answer. I was wondering if someone could help me debug my Java answer here: it can pass most of the test cases, but there's TLE for one test case... here's the link: docs.google.com/document/d/1NIKXhPHbNZd4YXzr9AqRXF5SpRL5v3Nnkn_M7NJNjZY/edit?usp=sharing