Below is the Question I was asked in an interview and I believe there are many solutions to this question but I want to know what can be the best solution (and stackoverflow is perfect for this 🙂 ).
Q: We have a tree like structure and have three threads. Now we have to perform three operations: Insert, Delete and lookup. How will you design this?
My Approach: I will take mutex for insert and delete operation as I want only one thread to perform at a time insert or delete. While in case of lookup I will allow all the three threads to enter in the function but keep a count(counting semaphore) so that insert and delete operation can’t be perform this time.
Similarly when insert or delete operation is going no thread is allowed to do lookup, same with the case for insert and delete.
Now he cross questioned me that as I am allowing only one thread at a time to insert so if two nodes on different leaf need to be inserted then still my approach will allow one at a time, this made be stuck.
Is my approach fine ?
What can be other approaches ?
How about like this? Similar to a traffic road block(broken paths).
leftClear_fandrightClear_findicatingclear-pathaheadLookup Operation:
conditional_waitand wait for signal.Insert Operation
Lookuptill you get to the location of insertion.parent_nodeand bothchild_nodes after checking their state.Delete/Insertcan happen on other valid unbroken pathsparent_nodeandchild_nodes.Delete Operation
same as Insert operation except that it deletes nodes.
PS: You can also maintain the details of the nodes under
InsertorDeleteprocess someplace else. Other operation can jump the broken paths if necessary/needed! It sounds complicated yet doable.