The Crazy Ruler
Updated
Problem statement
A connected country contains N cities numbered from 1 to N. Its N - 1 roads form a tree. City i has satisfaction value A[i].
The king evaluates one connected country by taking every distinct satisfaction value that appears in it exactly once and summing those distinct values.
Remove exactly one road to split the tree into two connected countries. Maximize the sum of the king's evaluations of the two resulting countries.
Return the maximum value modulo 10^9 + 7.
Function
maxSatisfactionSum(A: int[], edges: int[][]) → intExamples
Example 1
A = [2, 3, 2, 10, 10]edges = [[1, 4], [4, 3], [4, 2], [1, 5]]return = 27Removing road (1, 4) creates components with distinct-value sums 12 and 15. Their combined evaluation is 27.
Example 2
A = [2, 2, 3, 2, 10, 3]edges = [[1, 4], [4, 3], [4, 2], [1, 5], [5, 6]]return = 20Removing road (1, 4) produces distinct-value sets {2, 3, 10} and {2, 3}. Their sums are 15 and 5, for a total of 20.
Example 3
A = [1, 2, 2, 1, 2, 1, 2]edges = [[1, 4], [3, 2], [2, 7], [4, 2], [1, 5], [5, 6]]return = 6Removing road (1, 5) makes both components contain distinct values {1, 2}. Each evaluates to 3, so the answer is 6.
Constraints
2 ≤ N ≤ 10^5edges.length = N - 11 ≤ U, V ≤ N1 ≤ A[i] ≤ 10^5
More Microsoft interview questions
- Config Store with Priority OverridesPHONE SCREEN · Seen Nov 2026
- Authentication SystemOA · Seen Nov 2026
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Maximum String OperationsOA · Seen Nov 2026
- Minimum Cost to Process Priority TasksOA · Seen Nov 2026
- Most Visited Sprint MarkerOA · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Base Unit ConversionsAdobe · OAAdobe interview questions
- Count Prime Values in SubtreesVisa · OAVisa interview questions
- Part 1: The Narrowest KeyScale AI · OAScale AI interview questions
- Sewer Drainage PartitionTwo Sigma · OATwo Sigma interview questions
- Count Balanced Nodes in a Rooted TreeDRW · OADRW interview questions
- Chain of CommandIMC Trading · OAIMC Trading interview questions
- Construct Binary Tree S-ExpressionOptiver · OAOptiver interview questions
- Chain of CommandUber · OAUber interview questions
- Build and Render an Organization Role TreeStripe · OAStripe interview questions
- Kth Node in a Subtree DFSIntuit · OAIntuit interview questions
Microsoft interview experiences
- Microsoft OA Questions, October 2026: 4 Experiences
- Microsoft Experienced SWE Phone Screen: 1 Experience
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)