[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"doc-seo-391138-105":3,"detail-sidebar-cat-0-en-105":81,"doc-detail-391138-en":130},{"code":4,"msg":5,"data":6},0,"ok",{"site_id":7,"language":8,"slug":9,"title":10,"keywords":11,"description":12,"schema_data":13,"social_meta":74,"head_meta":76,"extra_data":78,"updated_unix":80},105,"en","technical-interview-questions-coding-assessment-tree-beauty-problem-and-good-subsequence-with-gcd-problem","Technical Interview Questions - Coding Assessment - Tree Beauty Problem and Good Subsequence with GCD Problem","","Technical Interview Questions for coding assessments covering two algorithmic problems. The first defines a rooted tree where nodes store values and a pair is GOOD when their product is a perfect square; beauty(u) counts such pairs inside each subtree and the task returns the total over all nodes modulo 109+7. The second problem uses an array with dynamic updates: a non-empty subsequence is GOOD when its length is less than n and the subsequence gcd equals p; after each query, determine whether any GOOD subsequence exists and count YES answers.",{"@graph":14,"@context":73},[15,34,56],{"@type":16,"itemListElement":17},"BreadcrumbList",[18,23,27,31],{"item":19,"name":20,"@type":21,"position":22},"https://docshare.wps.com","Home","ListItem",1,{"item":24,"name":25,"@type":21,"position":26},"https://docshare.wps.com/document/","Document",2,{"item":28,"name":29,"@type":21,"position":30},"https://docshare.wps.com/document/exam/","Exam",3,{"item":32,"name":10,"@type":21,"position":33},"https://docshare.wps.com/document/technical-interview-questions-coding-assessment-tree-beauty-problem-and-good-subsequence-with-gcd-problem/391138/",4,{"url":32,"name":10,"@type":35,"image":36,"author":41,"headline":10,"publisher":44,"fileFormat":47,"inLanguage":8,"description":12,"dateModified":48,"datePublished":49,"encodingFormat":47,"isAccessibleForFree":50,"interactionStatistic":51},"DigitalDocument",{"url":37,"@type":38,"width":39,"height":40},"https://docshare.wps.com/thumbnails/technical-interview-questions-coding-assessment-tree-beauty-problem-and-good-subsequence-with-gcd-problem/391138.png","ImageObject",300,407,{"name":42,"@type":43},"McQueen","Person",{"url":19,"name":45,"@type":46},"DocShare","Organization","application/pdf","2026-11-05","2026-09-25",true,{"@type":52,"interactionType":53,"userInteractionCount":55},"InteractionCounter",{"@type":54},"ViewAction",6,{"@type":57,"mainEntity":58},"FAQPage",[59,65,69],{"name":60,"@type":61,"acceptedAnswer":62},"How is a GOOD pair of nodes defined in the Tree Beauty Problem?","Question",{"text":63,"@type":64},"A pair (i, j) is GOOD if a[i] × a[j] is a perfect square, with 1 ≤ i \u003C j ≤ n.","Answer",{"name":66,"@type":61,"acceptedAnswer":67},"What does beauty(u) represent in the Tree Beauty Problem?",{"text":68,"@type":64},"beauty(u) is the number of GOOD node pairs fully contained within the subtree of node u.",{"name":70,"@type":61,"acceptedAnswer":71},"In the Good Subsequence with GCD Problem, what condition makes a subsequence GOOD?",{"text":72,"@type":64},"A non-empty subsequence is GOOD when its length is strictly less than n and the greatest common divisor of its elements is exactly p.","https://schema.org",{"og:url":32,"og:type":75,"og:title":10,"og:site_name":45,"og:description":12},"article",{"robots":77,"canonical":32},"index,follow",{"doc_id":79,"site_id":7},391138,1791148303,{"code":4,"msg":82,"data":83},"success",[84,88,92,95,100,104,109,114,119,122,126],{"id":22,"doc_module":4,"doc_module_name":25,"category_name":85,"show_sort_weight":86,"slug":87},"Story & Novel",90,"story-novel",{"id":26,"doc_module":4,"doc_module_name":25,"category_name":89,"show_sort_weight":90,"slug":91},"Literature",80,"literature",{"id":33,"doc_module":4,"doc_module_name":25,"category_name":29,"show_sort_weight":93,"slug":94},70,"exam",{"id":96,"doc_module":4,"doc_module_name":25,"category_name":97,"show_sort_weight":98,"slug":99},5,"Comic",60,"comic",{"id":55,"doc_module":4,"doc_module_name":25,"category_name":101,"show_sort_weight":102,"slug":103},"Technology",50,"technology",{"id":105,"doc_module":4,"doc_module_name":25,"category_name":106,"show_sort_weight":107,"slug":108},7,"Healthcare",40,"healthcare",{"id":110,"doc_module":4,"doc_module_name":25,"category_name":111,"show_sort_weight":112,"slug":113},8,"Research & Report",30,"research-report",{"id":115,"doc_module":4,"doc_module_name":25,"category_name":116,"show_sort_weight":117,"slug":118},9,"Religion & Spirituality",20,"religion-spirituality",{"id":117,"doc_module":4,"doc_module_name":25,"category_name":120,"show_sort_weight":117,"slug":121},"World Cup","world-cup",{"id":123,"doc_module":4,"doc_module_name":25,"category_name":124,"show_sort_weight":123,"slug":125},10,"Lifestyle","lifestyle",{"id":127,"doc_module":4,"doc_module_name":25,"category_name":128,"show_sort_weight":96,"slug":129},19,"General","general",{"code":4,"msg":82,"data":131},{"doc_id":79,"user_id":132,"nickname":42,"user_avatar":133,"doc_module":4,"category_id":33,"category_name":29,"doc_title":10,"doc_description":12,"doc_content":134,"file_id":135,"file_url":136,"file_type":137,"file_size":138,"view_count":55,"is_deleted":4,"is_public":22,"is_downloadable":22,"audit_status":22,"page_count":139,"language":140,"language_code":8,"site_id":7,"html_lang":8,"table_of_contents":141,"faqs":142,"seo_title":143,"seo_description":12,"update_tm":144,"read_time":107,"preview_page_count":4},5909890329169,"https://ap-avatar.wpscdn.com/davatar_9964176cb1d06d4a9deccf72a44ae3dc","Technical Interview Questions-Coding Assessment  \n1 EASY  \n1.1 Tree Beauty Problem  \n1.1.1 Question Description  \nYou are given a tree of n nodes, each node has a value a [i] written on it. The tree is rooted at node 1 .  \nA pair of nodes i,j (where 1 ≤ i \u003C j ≤ n) is considered GOOD if a[i] ×a[j] is a perfect square. We define beauty(u) as the number of good pairs of nodes in the subtree of u. Your task is to find the sum of beauty(i) for each 1 ≤ i ≤ n. Return the sum of these values modulo 109 + 7.  \n1.1.2 Function Description  \nName : get_ans  \nParameters:  \n-n (INTEGER): The size of the tree  \n-par (INTEGER ARRAY): The parent array par[1] = 0  \n-a (INTEGER ARRAY): The values written on the nodes  \nReturn : INTEGER  \n-The sum of beauty of each node modulo 10^9 + 7  \n1.1.3 Constraints  \n• 1 ≤ n ≤ 105 (Size of the tree)  \n• 0 ≤ par[i] ≤ n (Valid parent indices)  \n• par[1] = 0 (Root is node 1, which has no parent)  \n• 1 ≤ a [i] ≤ 109 (Node values)  \n• All values must be considered for perfect square pairs  \n• Result must be modulo 109 + 7  \n1.1.4 Input Format  \nLine 1: Integer n  \n-Size of the tree  \nNext n lines: par[0], par[1], . . . , par[n-1]  \n-Parent array of the rooted tree  \n-par[0] is typically 0 (root indicator)  \n-par[i] is the parent of node i (1-indexed) Next n lines: a[0], a[1],   , a[n-1]  \n-Values written on each node  \n-Each value is between 1 and 10^9  \n1.1.5 Output Format  \nSingle Integer: The sum of beauty values for all nodes modulo 109 + 7  \n1.1.6 Sample Test Case 1  \nInput:  \n5 0  \n1 1  \n2 2  \n2 3  \n6  \n12  \n27  \nOutput:  \n6  \nTree Structure:  \n1(2)  \n/ \\  \n2(3) 3(6)  \n/ \\  \n4(12) 5(27)  \nNode Values:  \n• Node 1: 2  \n• Node 2: 3  \n• Node 3: 6  \n• Node 4: 12  \n• Node 5: 27  \nComputing Beauty for Each Node:  \n• beauty(5) = 0 (Only one node in subtree of 5; no pairs possible)  \n• beauty(4) = 0 (Only one node in subtree of 4; no pairs possible)  \n• beauty(3) = 0 (Only one node in subtree of 3; no pairs possible)  \n• beauty(2) = 3 (Subtree of 2: {2, 4, 5} with values {3, 12, 27})  \n– Pair (2, 4): 3 × 12 = 36 = 62 (Perfect square)  \n– Pair (2, 5): 3 × 27 = 81 = 92 (Perfect square)  \n– Pair (4, 5): 12 × 27 = 324 = 182 (Perfect square)  \n• beauty(1) = 3 (Entire tree: {1, 2, 3, 4, 5} with values {2, 3, 6, 12, 27})  \n– Good pairs are the same three from subtree of 2  \nSum of beauty values:  \nbeauty(1) + beauty(2) + beauty(3) + beauty(4) + beauty(5) = 3 + 3 + 0 + 0 + 0 = 6  \nAnswer: 6  \n1.1.7 Sample Test Case 2  \nInput:  \n2 0  \n1 4  \n9  \nOutput:  \n1  \nTree Structure:  \n1(4)  \n|  \n2(9)  \nComputing Beauty for Each Node:  \n• beauty(2) = 0 (Only one node in subtree of 2)  \n• beauty(1) = 1 (Subtree of 1: {1, 2} with values {4, 9})  \n– Pair (1, 2): 4 × 9 = 36 = 62 (Perfect square)  \nSum of beauty values: 1 + 0 = 1  \nAnswer: 1  \n1.1.8 Sample Test Case 3  \nInput:  \n3 0  \n1 1  \n2 8  \n18  \nOutput:  \n3  \nTree Structure:  \n1(2)  \n/ \\  \n2(8) 3(18)  \nComputing Beauty for Each Node:  \n• beauty(2) = 0 (Only one node in subtree of 2)  \n• beauty(3) = 0 (Only one node in subtree of 3)  \n• beauty(1) = 3 (Subtree of 1: {1, 2, 3} with values {2, 8, 18})  \n– Pair (1, 2): 2 × 8 = 16 = 42  \n– Pair (1, 3): 2 × 18 = 36 = 62  \n– Pair (2, 3): 8 × 18 = 144 = 122  \nSum of beauty values: 3 + 0 + 0 = 3  \nAnswer: 3  \n2 MEDIUM  \n2.1 Good Subsequence with GCD Problem  \n2.1.1 Question Description  \nYou are given an array a of length n and an integer p.  \nA non-empty subsequence of a is considered GOOD if the following conditions hold:  \n1. The length of the subsequence is strictly less than n.  \n2. The greatest common divisor of the elements of the subsequence is exactly p.  \nYou have to process q queries of the following form:  \ni j : replace a [i] with j (where 1 ≤ i ≤ n and 1 ≤ j ≤ 105) .  \nAfter each query, you must check if there exists any good subsequence. If it exists, the answer to that query is YES.  \nFind the number of queries that were answered YES.  \n2.1.2 Function Description  \nName : get_ans  \nParameters:  \n-n (INTEGER): The size of the","cbCaiiz0HCyg1Rex","https://ap.wps.com/l/cbCaiiz0HCyg1Rex","pdf",169201,16,"English","# Tree Beauty Problem\n## Question Description\n## Function Description\n## Constraints\n## Input Format\n## Output Format\n## Sample Test Cases\n# Good Subsequence with GCD Problem\n## Question Description\n## Function Description\n## Constraints\n## Input Format\n## Output Format\n## Sample Test Cases","[{\"question\":\"How is a GOOD pair of nodes defined in the Tree Beauty Problem?\",\"answer\":\"A pair (i, j) is GOOD if a[i] × a[j] is a perfect square, with 1 ≤ i \\u003c j ≤ n.\"},{\"question\":\"What does beauty(u) represent in the Tree Beauty Problem?\",\"answer\":\"beauty(u) is the number of GOOD node pairs fully contained within the subtree of node u.\"},{\"question\":\"In the Good Subsequence with GCD Problem, what condition makes a subsequence GOOD?\",\"answer\":\"A non-empty subsequence is GOOD when its length is strictly less than n and the greatest common divisor of its elements is exactly p.\"}]","Technical Interview Questions - Coding Assessment - Tree Beauty Problem and Good Subsequence with GCD Problem | PDF",1790349652]