in a interview the interviewer will ask the question for the porblem """ Locking The…
- User
in a interview the interviewer will ask the question for the porblem """
Locking The Tree of Space You have a world map represented as an M-Ary tree (sample tree below)
asterisk_green.svgIn graph theory, an m-ary tree (for nonnegative integers m) (also known as n-ary, k-ary or k-way tree) is an arborescence (or an ordered tree) in which each node has no more than m children. A binary tree is the special case where m = 2, and a ternary tree is another case with m = 3 that limits its children to three.
asterisk_green.svgIn graph theory, an arborescence is a directed graph in which, for a vertex u (called the root) and any other vertex v, there is exactly one directed path from u to v. A graph is a collection of vertices (also known as nodes) and edges that connect these vertices. A tree is a special type of graph that is connected and acyclic, meaning that there are no cycles in the graph. In a tree, there is a unique path between any two vertices, and there is a single vertex called the root that is used as the starting point for traversing the tree.
Sample Tree
You need to define three operations on it:
- lock(X, uid)
- unlock(X, uid)
- upgradeLock(X, uid) where X the name of a node in the tree (that would be unique) and uid is the user who is performing the operation. Input format: x is country name (string) and uid is user number (number).
Here are the definitions for the operations:
lock(x, uid) Lock takes an exclusive access on the subtree rooted at X. It is formally defined like this: Once lock(x, uid) succeeds, then: lock(A, anyUserid) should fail (returns false), where A is a descendent of X,
lock(B, anyUserld) should fail (returns false), where X is a descendent of B,
Lock operation cannot be performed on a node which is already locked i.e. lock(x, anyUserld) should fail (returns false).
asterisk_green.svgExclusive access means that only one user can lock a node and its descendants at a time, and no other user can lock any node that is related to the locked node by ancestry or descent. This ensures that there is no conflict or interference between different users who want to perform operations on the tree. unlock(X, uid) Unlock reverts what was done by the lock operation. It can only be called on same node on which user uid had called a Lock on before. Returns true if it is successful.
upgradeLock(x, uid) It helps the user uid upgrade their lock to an ancestor node. It is only possible if the node X already has locked descendants and all of them are only locked by the same user uid. Upgrade should fail if there is any node which is descendant of X that is locked by a different user. Successful Upgrade will 'lock' the node. upgradeLock call shouldn't violate the consistency model that Lock/Unlock function requires.
Notes
The number of nodes in the tree N is very large. So, optimize the time complexity for the above algorithms. The below section contains the input format. The first line contains the number of Nodes in the tree (N). The second line contains number of children per node (value m in m-ary Tree). The third line contains number of queries (Q). Next N lines contains the NodeName (string) in the m-Ary Tree. Next Q lines contains queries which are in format: Operation Type NodeName Userld Operation Type 1 for lock 2 for unlock 3 for upgradeLock NodeName - Name of any node (unique) in m-Ary Tree. Userld - Integer value representing a unique user. Example input
- Assistant