Lockable Tree - Google Interview Question

Lockable tree is a great programming interview question asked by Google, and it is a very well thought out one. A lockable tree is a tree with nodes that can be locked if none of the ancestors or descendants is locked. In the question, we are asked to implement locking/unlocking operations that should run in O(h) time where h is the height of the tree. Lock/unlock methods do not need to be thread-safe.
This is a very well-crafted interview question by Google. Both the requirements and the question itself are quite clear, which is a rarity in the industry. Often, the interviewers will intentionally make the question a little obscure, so they can observe how you do your requirements analysis and if you can communicate with the interviewers clearly. However, in this case, the requirements are clear cut, which I think reflects how Google operates. It is a medium difficulty question. But a fair knowledge of tree data structures is necessary to come up with a clean and concise solution. Do not worry though, I have a video comping up on general tree structures soon. And finally, solution code for this problem (along with its tests) is on GitHub, and the link is below.
Note: 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.
Solution code to examples are available on:
• github.com/soygul/QuanticDev/blob/master/algorithms/trees/lockable-tree/lockable-tree.js
If you can read the article version of this video at:
• quanticdev.com/algorithms/trees/lockable-tree
My "Algorithms" Playlist for all other algorithm questions & answers:
• kzread.info/head/PLlPRnMzqjADqDZFBqdwzbIjf71h8xYs4x
- - - - - - - - - - -
quanticdev
quantic_dev
quanticdev.com

Пікірлер: 81

  • @QuanticDev
    @QuanticDev4 жыл бұрын

    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

    3 жыл бұрын

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

  • @QuanticDev

    @QuanticDev

    3 жыл бұрын

    @@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

    3 жыл бұрын

    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

    3 жыл бұрын

    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

    3 жыл бұрын

    @@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.

  • @harshalgupta1613
    @harshalgupta16132 жыл бұрын

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

  • @dmymal
    @dmymal3 жыл бұрын

    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

    3 жыл бұрын

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

  • @himanigupta6001
    @himanigupta60012 жыл бұрын

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

  • @sejalkale67
    @sejalkale677 ай бұрын

    How to make it thread safe ??

  • @adityarajora7219
    @adityarajora72193 жыл бұрын

    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.

  • @abdullahelkammar
    @abdullahelkammar3 жыл бұрын

    Very well explained, thank you!

  • @harshsharma4351

    @harshsharma4351

    2 жыл бұрын

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

  • @sunilg8839
    @sunilg88393 жыл бұрын

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

  • @QuanticDev

    @QuanticDev

    3 жыл бұрын

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

  • @akihiro562
    @akihiro5623 жыл бұрын

    Can u please share a thread safe version of the code

  • @QuanticDev

    @QuanticDev

    3 жыл бұрын

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

  • @ankitkr09
    @ankitkr092 жыл бұрын

    how to make it thread safe without using synchronous?

  • @oswinrozario7442

    @oswinrozario7442

    Жыл бұрын

    did you get the solution?

  • @williamalexander5371
    @williamalexander53714 жыл бұрын

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

  • @QuanticDev

    @QuanticDev

    4 жыл бұрын

    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:)

  • @pankajkaushik913
    @pankajkaushik9132 жыл бұрын

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

  • @QuanticDev

    @QuanticDev

    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

    2 жыл бұрын

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

  • @QuanticDev

    @QuanticDev

    2 жыл бұрын

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

  • @pankajkaushik913

    @pankajkaushik913

    2 жыл бұрын

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

  • @pankajkaushik913

    @pankajkaushik913

    2 жыл бұрын

    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

  • @vaishnavidatrak4730
    @vaishnavidatrak47303 жыл бұрын

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

  • @QuanticDev

    @QuanticDev

    3 жыл бұрын

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

  • @harshsharma4351

    @harshsharma4351

    2 жыл бұрын

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

  • @bhavneetsingh2000
    @bhavneetsingh20002 жыл бұрын

    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

    2 жыл бұрын

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

  • @CodeMap01

    @CodeMap01

    2 жыл бұрын

    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?

  • @Happy-on2is
    @Happy-on2is3 жыл бұрын

    That's really helpful thank you 😁👍

  • @harshsharma4351

    @harshsharma4351

    2 жыл бұрын

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

  • @shrad6611
    @shrad66113 жыл бұрын

    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

  • @iamritam

    @iamritam

    2 жыл бұрын

    did you able to solve it?

  • @mickyman753

    @mickyman753

    2 жыл бұрын

    have you uploaded it somewhere

  • @ankitakataria4114

    @ankitakataria4114

    Жыл бұрын

    Can you share it now

  • @suhailahmad2586
    @suhailahmad25863 жыл бұрын

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

  • @QuanticDev

    @QuanticDev

    3 жыл бұрын

    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

  • @rajkrishnabolisetty2596
    @rajkrishnabolisetty25963 жыл бұрын

    Can you provide this code in C++ please

  • @QuanticDev

    @QuanticDev

    3 жыл бұрын

    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

    3 жыл бұрын

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

  • @QuanticDev

    @QuanticDev

    3 жыл бұрын

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

  • @mayankprakash9651
    @mayankprakash96513 жыл бұрын

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

  • @QuanticDev

    @QuanticDev

    3 жыл бұрын

    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.

  • @MadForCs16
    @MadForCs163 жыл бұрын

    All heroes don't wear capes.

  • @QuanticDev

    @QuanticDev

    3 жыл бұрын

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

  • @MadForCs16

    @MadForCs16

    3 жыл бұрын

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

  • @MadForCs16

    @MadForCs16

    3 жыл бұрын

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

  • @QuanticDev

    @QuanticDev

    3 жыл бұрын

    My pleasure.

  • @subbareddy285

    @subbareddy285

    3 жыл бұрын

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

Келесі