top of page

Looking to master object-oriented and system design for tech interviews or career growth?

  • Improve your system design and machine coding skills.

  • Study with our helpful resources.

  • Prepare for technical interviews and advance your career.

**We're in beta mode and would love to hear your feedback.

Locking Unlocking of N-ary Tree

Given an n-ary tree of resources arranged hierarchically such that the height of the tree is O(log N) where N is a total number of nodes (or resources). A process needs to lock a resource node in order to use it. But a node cannot be locked if any of its descendant or ancestor is locked.


Following operations are required for a given resource node:


Lock(string X, int id):

1. if X is already locked then you cannot lock it, so return false

2. if any of its descendants is locked, you cannot lock it, so return false

3. if any of its ancestors is locked, you cannot lock it, so return false

4. If none of the above conditions are met, lock X with id


Unlock(string X, int id):

1. if X is not locked return false

2. if X is locked by some different id, then return false

3. otherwise unlock and return true


UpgradeLock(string X, int id):

1. If X is locked return false

2. If it has no locked Descendents return false

3. If it has locked descendants by more than one id then return false

4. If the above condition doesn't satisfy then unlock every locked descendants and lock X



Solution

Code in C++


4 comentarios


The code is thread safe or not ?


Me gusta
rockstar
rockstar
20 ene
Contestando a

did you get the thread safe code? please help

Me gusta
bottom of page