what is the timecomplexxity like will it pas this questions Problem statement You are…
- User
what is the timecomplexxity like will it pas this questions Problem statement You are given a complete, balanced M-Ary Tree and must support Q queries. There are 3 kinds of queries. Return true or false depending on whether the query was successful.
Lock(v, id) - Lock vertex v for user - id Unlock(v, id) - If vertex v is locked by the same id, unlock it. Upgrade(v, id) - If v is unlocked and has at least one locked vertex in it's subtree and every locked vertex in the subtree of v is locked by id, unlock them and lock v instead. Further, here are some additional constraints
A vertex cannot be locked if it has any locked ancestors or descendants, by any ID. When a vertex is upgraded, it's locked descendants are automatically unlocked. An upgrade operation is not possible if the vertex is already locked or has any locked ancestors An unlock operation is only possible if the vertex is already locked and locked by the same id P.S. - The question was very badly stated and not a single constraint was mentioned openly on the problem statement. A lot of these constraints had to be deduced by supplying custom input and observing the expected output. There were no constraints on the size of the variables.
anotjer version Question 1: Locking the Tree of Space You have a world map represented as an M-Ary tree (sample tree below):
You need to define three operations on it: lock(X, uid) unlock(X, uid) upgradeLock(X, uid) Where X is the name of a node in the tree (which is unique), and uid is the user performing the operation. Definitions for the Operations: Lock(X, uid): Lock takes exclusive access on the subtree rooted at X. Once lock(X, uid) succeeds: lock(A, anyUserId) should fail (returns false), where A is a descendant of X. lock(B, anyUserId) should fail (returns false), where X is a descendant of B. Lock operation cannot be performed on a node that is already locked, i.e., lock(X, anyUserId) should fail (returns false). Unlock(X, uid): Unlock reverts what was done by the lock operation. It can only be called on the same node on which user uid had called a lock before. Returns true if successful. UpgradeLock(X, uid): This 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 locked by the same user uid. Upgrade should fail if there is any node which is a descendant of X that is locked by a different user. A successful upgrade will "lock" the node. UpgradeLock must not violate the consistency model required by Lock/Unlock. Notes: The number of nodes in the tree, N, is very large, so optimize the time complexity for the above algorithms. Input Format: The first line contains the number of nodes in the tree (N). The second line contains the number of children per node (m in an m-ary tree). The third line contains the number of queries (Q). Next N lines contain the NodeName (string) in the m-Ary Tree. The next Q lines contain queries in the format: OperationType NodeName UserId. Operation Type: 1 for Lock 2 for Unlock 3 for UpgradeLock NodeName: The name of any node (unique) in the m-Ary Tree. UserId: An integer representing a unique user. Example Input: 7 2 3 World Asia Africa China India South Africa Egypt 1 China 9 2 India 9 3 Asia 9
- Assistant