hye i have written this code and interviewr asked me to implemtn cas and set and test…
- User
hye i have written this code and interviewr asked me to implemtn cas and set and test threadsafe to the LOCK method in this code, as it is for iterview dointit use modules threading or any and also ignore the GIL as it is for showing logic and interview purposed 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. """ class Node: def init(self, val): self.val = val self.uid = None self.parent = None self.children = [] self.locked_descendant_count = 0 self.isLocked = False
class M_Arr_Tree: def init(self, n, m, name_list): self.node_list = [] self.node_map = {}
for name in name_list: node = Node(name) self.node_list.append(node) self.node_map[name] = node
for i in range(n): for j in range(m): child_idx = m * i + j + 1 if child_idx < n: self.node_list[i].children.append(self.node_list[child_idx]) self.node_list[child_idx].parent = self.node_list[i] def _update_lock_count_upwards(self, node, delta): current = node.parent while current: current.locked_descendant_count += delta current = current.parent
def Lock(self, node, uid): if node.isLocked or node.locked_descendant_count > 0: return 'false'
Check if any ancestors are locked
current = node.parent while current: if current.isLocked: return 'false' current = current.parent
- Assistant