Lockable Tree - Google Interview Question

Sdílet
Vložit
  • čas přidán 5. 09. 2024

Komentáře • 82

  • @QuanticDev
    @QuanticDev  Před 4 lety +7

    Note: I forgot to mention that lock/unlock methods do not need to be thread-safe. A real-world implementation of tree locking in databases would mostly have to be thread-safe, but I think it would be very domain-specific to be a general interview question.
    6:38 - Space complexity of the "lock()" function is O(1) but space complexity of the entire tree goes up by O(n) during its creation due to the added fields. Don't forget to explain this detail, as I forgot here!

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

      but JusPay ask same question that if it we can make it a thread safe

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

      @@shrad6611 I guess this was an interview for a database heavy position. If you type the question, I can give you an answer.

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

      Let's say you are running the lock/unlock in a multi core machine. Now you want to let multiple threads to run lock() simultaneously.
      As we saw in your part of question , locking a node has multiple validations inside. Will doing lock on two nodes cause a race condition.
      If yes, how will you solve it.
      In short, how do make the lock() function thread safe?
      Multiple threads running it simultaneously shouldn't not affect the correctness.
      Note that the critical sections should be granular.
      Don't create any big transaction blocks that will make parallelism suffer.
      Tomorrow is my hackathon round for the same company can you please send me some solution for this question because they might ask me same question because they doing same from past 4 years

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

      and this question is not database heavy position question this is for freshers Software developer position they ask this question in hackathon every year(hackathon is a second interview round in JusPay)

    • @QuanticDev
      @QuanticDev  Před 3 lety

      @@shrad6611 Ok it's not a hard one as I thought it would be. You just need to use read/write mutexes in the branch of the tree that you are trying to lock or checking for lockability. If you are checking to see if anything is locked, use a read mutex. If you are trying to lock something, use a read/write mutex. You only need to lock one branch since as you saw in the video, siblings of a node to be locked is irrelevant to it's lockability.

  • @ashutoshraj7171
    @ashutoshraj7171 Před 7 dny +1

    How to make thread safe, if multiple lock is happen simultaneously

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

    @QuanticDev, can you share any idea how to make lock() and unlock() thread-safe?

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

    Hi @QuanticDev, thank you for the video. I am wondering why you only check for ancestors if we also want to check for descendants?

    • @QuanticDev
      @QuanticDev  Před 3 lety

      In 4:26, you can watch me asking the same question than answering it in details.

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

    Hey loved it, would be great if you can roll out an article or video on how to make it thread safe

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

      I am planning to but it's a bit low priority since thread-safe version of this is a database specific topic and not really interesting to general audience.

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

      Makes sense! Somehow this question is coming in a lot of interviews in india

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

      @@pankajkaushik913 Is it a database developer company? Or a big-data company maybe?

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

      @@QuanticDev nope it's a fintech company, and they are asking this question from fresher's

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

      Would love to get your insights on this, I am stuck on it for 2 days. Most probably it will appear in my interview also. I have checked solutions in other comments but it's not quite clear from it

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

    I will share java version of the code after tomorrow lol
    because tomorrow is my test and my competitors also watched the code and defeat me hahahaha lol

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

    Can u please share a thread safe version of the code

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

      You can find it in other comments of the video. Thread-safe implementation will greatly depend on the question.

  • @himanigupta6001
    @himanigupta6001 Před 2 lety

    can you plz give the javascript code for upgrade function as well

  • @sejalkale67
    @sejalkale67 Před 9 měsíci

    How to make it thread safe ??

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

    Nice, informative video! How much time would you expect to give a candidate to solve this?

    • @QuanticDev
      @QuanticDev  Před 4 lety +1

      I would expect about half an hour including discussion + code + tests (optional but good). If I was asking the more complex multi-threaded version of this question, I would expect ~45 minutes. Obviously I would ask a variation of this question so that the candidate would not end up typing everything in 5 minutes from the memory:)

  • @abdullahelkammar
    @abdullahelkammar Před 4 lety +1

    Very well explained, thank you!

    • @harshsharma4351
      @harshsharma4351 Před 2 lety

      Sir if you have time could you please explain this question.

  • @ankitkr09
    @ankitkr09 Před 2 lety

    how to make it thread safe without using synchronous?

  • @adityarajora7219
    @adityarajora7219 Před 3 lety

    I need help from you.
    given directed graph as input with two nodes say A, B. Need to find the minimum number of node deletes so that we can not reach A if we start to move from B to A.

  • @sunilg8839
    @sunilg8839 Před 3 lety

    What if we call two nodes, will we encounter any inconsistency in this code??

    • @QuanticDev
      @QuanticDev  Před 3 lety

      My sample code is for single-threaded use only. You could implement thread-safety with some simple read/write mutexes though.

  • @vaishnavidatrak4730
    @vaishnavidatrak4730 Před 4 lety +1

    could you please convert the code into python or c++?

    • @QuanticDev
      @QuanticDev  Před 4 lety +1

      I created a task here and will do it when I have the time: github.com/soygul/QuanticDev/issues/48

    • @harshsharma4351
      @harshsharma4351 Před 2 lety

      Ma'am if you have time could you please explain this question.

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

    Explained well but can you write the code in C++?

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

      I created a task for it: github.com/soygul/QuanticDev/issues/48
      I'm planning on adding a code translator tool to do a rough translation of all JS code to everything else for me. We'll see how it goes.

  • @Happy-on2is
    @Happy-on2is Před 4 lety +1

    That's really helpful thank you 😁👍

    • @harshsharma4351
      @harshsharma4351 Před 2 lety

      Sir if you have time could you please explain this question.

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

    Could you share how to implement multithreading in this question without using semaphores or mutex, companies are specifically asking not to use them?

    • @CodeMap01
      @CodeMap01 Před 2 lety

      can you please give me hint how to do thread safe in lock ?

    • @CodeMap01
      @CodeMap01 Před 2 lety

      Bro in part B Basically the idea is, if the nodes we wanna lock doesnot intersect that means are not ancestors or descendents of each other, then they can be locked simultaneously. To ensure this we need to synchronize the noed so, that it cannot be accesses by other node.But i cant get the logic behind this solution . can you please give me any hint?

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

    Can you provide this code in C++ please

    • @QuanticDev
      @QuanticDev  Před 4 lety

      I will add an auto code translator tool. Until then, you can use a JS->C++ converter yourself. You'll have to Google a bit to find a good one.

    • @rajkrishnabolisetty2596
      @rajkrishnabolisetty2596 Před 4 lety +1

      @@QuanticDev sir this code is not working for the siblings condition , what to do for that please help me out

    • @QuanticDev
      @QuanticDev  Před 4 lety +1

      Sibling condition? There is no condition for siblings in the question.

  • @suhailahmad2586
    @suhailahmad2586 Před 4 lety +1

    can u give a thread-safe solution to the problem if u have solved it..

    • @QuanticDev
      @QuanticDev  Před 4 lety +1

      Easy solution is just to use a Read/Write lock (mutex) and prevent concurrent lock attempts. A mutex-free thread-safe implementation would have to be more elaborate so I didn't code anything on that yet.
      Edit: This looks to be a good implementation which treats the entire tree like an immutable data structure: github.com/npgall/concurrent-trees

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

    All heroes don't wear capes.

    • @QuanticDev
      @QuanticDev  Před 3 lety

      I'm kinda amazed that companies are still using this question.

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

      @@QuanticDev Yes. I was preparing for Juspay where I encountered this question.

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

      Btw I am finding your series very helpful. Thank you very much.

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

      My pleasure.

    • @subbareddy285
      @subbareddy285 Před 3 lety

      @@MadForCs16 bro can i contact u for details of juspay hackathon