Kali_prasad

intuit OA 2025 dec 17

Dec 21st, 2025
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.34 KB | None | 0 0
  1.  
  2.  
  3. '''
  4.  
  5. given you a tree with 1 as root node ,with parent info
  6. when a command sent by ith node
  7. that command travels through that node to its child node
  8. via ordered dfs manner
  9. that means if you have x children in dfs you will try to process them in ordered manner
  10. given you a list of queries
  11. in each query you will be given commandNode , xdistanceTimeNode
  12. so if you pass a command from given commandNode , which node will get that in x distance time
  13. if it is not possible return -1
  14.  
  15. ============================================================================================================================================
  16.  
  17. say you are given a tree
  18.  
  19. like
  20. 1 2 3 4 5 6 7 8 9
  21. -1 1 1 1 3 5 3 5 7//parentInfo
  22.  
  23. so the tree would look like
  24.  
  25.                1
  26.  
  27.      2         3          4
  28.  
  29.            5       7
  30.  
  31.          6   8       9
  32.  
  33. lets have adjList
  34. and sort all the children in each node
  35. as question demands
  36. now if you do the dfs and log it in an array the dfsArray would look like
  37.  
  38. 1 2 3 5 6 8 7 9 4
  39.  
  40. lets assume the query you got is 3,2
  41. so command sent from 3 and what is the second node it recieves it
  42. check the dfs array it says 5
  43.  
  44. but you need to be careful you should not enter into other substrees
  45. how can you do that if you know the subtree population then you can easily do this
  46.  
  47. you know how to get the population of subtree easily
  48. that's it with the help of ordered dfs you will know wether the query exceeded boundary to send -1
  49. or you have a valid node which recieves the command as ith in the line from commandNode
  50.  
  51. '''
  52. from collections import defaultdict
  53. class Solution:
  54.  
  55.     def dfs(self,adjList,node,subtreepopulation,dfsarr):
  56.         for child in adjList[node]:
  57.             dfsarr.append(child)
  58.             self.dfs(adjList,child,subtreepopulation,dfsarr)
  59.         for child in adjList[node]:
  60.             subtreepopulation[node] += subtreepopulation[child]
  61.        
  62.         return
  63.        
  64.  
  65.  
  66.     def ithNodeToRecieveCommand(self,parent,queries):
  67.         n = len(parent)
  68.         adjList = defaultdict(lambda:[])
  69.         for i in range(1,n):
  70.             adjList[parent[i]].append(i+1)
  71.         for k,arr in adjList.items():
  72.             arr.sort()
  73.         subtreepopulation = [1 for i in range(n+1)]
  74.         dfsarr =[1]
  75.         self.dfs(adjList,1,subtreepopulation,dfsarr)
  76.         # print(dfsarr)
  77.         indmapp = defaultdict(lambda:-1)
  78.         for i in range(n):
  79.             indmapp[dfsarr[i]] = i
  80.         ans = []
  81.         # print(indmapp)
  82.  
  83.         for q in queries:
  84.             if subtreepopulation[q[0]]<q[1]:
  85.                 ans.append(-1)
  86.             else:
  87.                 ans.append(dfsarr[indmapp[q[0]]+(q[1]-1)])
  88.         return ans
  89.  
  90.  
  91. def main():
  92.     solution = Solution()
  93.    
  94.     # Test cases: (parent_array, queries, expected_result, description)
  95.     test_cases = [
  96.         (
  97.             [-1, 1, 1, 1, 3, 5, 3, 5, 7],
  98.             [[3, 2]],
  99.             [5],
  100.             "Example from problem - command from node 3, 2nd node receives it"
  101.         ),
  102.         (
  103.             [-1, 1, 1, 1, 3, 5, 3, 5, 7],
  104.             [[1, 1], [1, 5], [3, 1]],
  105.             [1, 6, 3],
  106.             "Multiple queries from root and subtree"
  107.         ),
  108.        
  109.     ]
  110.    
  111.     print("Running test cases...\n")
  112.     passed = 0
  113.     failed = 0
  114.    
  115.     for i, (parent, queries, expected, description) in enumerate(test_cases, 1):
  116.         try:
  117.             result = solution.ithNodeToRecieveCommand(parent, queries)
  118.             status = "✓ PASS" if result == expected else "✗ FAIL"
  119.            
  120.             if result == expected:
  121.                 passed += 1
  122.             else:
  123.                 failed += 1
  124.            
  125.             print(f"Test {i}: {status}")
  126.             print(f"  Parent: {parent}")
  127.             print(f"  Queries: {queries}")
  128.             print(f"  Description: {description}")
  129.             print(f"  Expected: {expected}")
  130.             print(f"  Got:      {result}")
  131.         except Exception as e:
  132.             failed += 1
  133.             print(f"Test {i}: ✗ ERROR")
  134.             print(f"  Parent: {parent}")
  135.             print(f"  Queries: {queries}")
  136.             print(f"  Description: {description}")
  137.             print(f"  Error: {e}")
  138.         print()
  139.    
  140.     print(f"Results: {passed} passed, {failed} failed out of {len(test_cases)} tests")
  141.  
  142.  
  143. if __name__ == "__main__":
  144.     main()
  145.  
Advertisement
Add Comment
Please, Sign In to add comment